由于SQL正则替换不支持反向引用使用PHP读取替换并重新写入数据库:
<?php
$db = 'typecho';
$user = 'root';
$pass = 'password';
try {
$dbo = new PDO('mysql:host=localhost;dbname='.$db, $user, $pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8';"));
} catch (PDOException $e) {
$error = $e->getMessage();
die("PDO Execute Error : ".$error."\n");
}
$dbo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT cid,text FROM typecho_contents";
$stmt = $dbo->query($sql);
//$result = $stmt->fetch(PDO::FETCH_ASSOC);
//var_dump($result);
while (list($cid,$text)=$stmt->fetch(PDO::FETCH_NUM)){
echo $cid."\n";
$pattern = "/<code\s+?lang=[\'\"](\w+?)[\'\"]>/i";
//preg_match($pattern, $text, $matches);
//print_r($matches);
$text = preg_replace($pattern, "```\$1", $text);
$text = preg_replace('/<code>/', "```", $text);
$text = preg_replace('/<\/code>/', "```", $text);
$text = preg_replace('/^/', '<!--markdown-->', $text);
$st = $dbo->prepare("UPDATE typecho_contents set `text` = ? where cid = ?");
$st->bindParam(1, $text);
$st->bindParam(2, $cid);
$st->execute();
}
标签:none
你网站用 Typecho 建的吗?如何让编辑器支持原生的图片插入,并且支持文字居中,插入视频之类的?