让Crayon Syntax Highlighter与PHP-Markdown共存

由于Crayon Syntax Highlighter中定义了如[cpp]等短标签,Markdown语法中也有[]用于表示超链接,因此很容易出错。最简单的解决办法还是去掉[cpp]之类短标签的支持,改用【crayon lang="cpp"】的长标签。但是之前的文章还得一篇一篇改,考虑到之前用到的短标签不是很多,干脆直接暴力替换掉。

PHP代码如下(需要把替换成[):

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?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();
$replace = array();
$reparr = array(
  '[py]'     => '【crayon lang="python"]',
  '[cpp]'    => '【crayon lang="cpp"]',
  '[pas]'    => '【crayon lang="pascal"]',
  '[php]'    => '【crayon lang="php"]',
  '[html]'   => '【crayon lang="html"]',
  '[/py]'    => '【/crayon]'
  '[/cpp]'   => '【/crayon]',
  '[/pas]'   => '【/crayon]',
  '[/php]'   => '【/crayon]',
  '[/html]'  => '【/crayon]',
);
$count = 0;
foreach ($reparr as $key => $value)
{
  $search[$count] = $key;
  $replace[$count] = $value;
  ++$count;
}
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();
?>

下面做几个测试:

超链接

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

int main()
{
  static int arr[100];
  int c; // This is a special var
  cout << "Input a index";
  cin >> c;
  cout << arr[c] << endl; // arr[c] is special, too!
}

Comments