修复数据库中被SyntaxHighlighter替换的特殊符号

自从换上了Crayon Syntax Highlighter,发现原来<>&“符号被WP替换成了&lt;  &gt; &quot; &amp; 而Crayon Syntax Highlighter又不需要转换,只好转回来,于是写了下面这个脚本,成功地替换回来了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
header("Content-type:text/plain");
$conn = new mysqli("localhost", "root", "1234", "wordpress");
$conn->set_charset("utf8");
$result = $conn->query("SELECT post_content, ID FROM wp_posts");
$stmt = $conn->prepare("UPDATE wp_posts SET post_content = ? WHERE ID = ?");


$search  = array("&lt;", "&gt;", "&quot;", "&amp;");
$replace = array("<"   , ">"   , """    , "&"    );
while ($row = $result->fetch_array())
{
  $id = $row['ID'];
  $post_content = str_replace($search, $replace, $row['post_content']);
  $stmt->bind_param('si', $post_content, $id);
  if (!$stmt->execute())
  {
    die("n[ERROR!]".$id."n");
  }
  else
  {
    echo "[Success]".$id."n";
  }
}


$result->free();
$conn->close();
?>

Comments