今天我终于把blog(WordPress)迁移到了新的VPS上, 由于环境的不一致(原来是Apache+mod_php, 现在是lighttpd+php_cgi), 中间出现不少问题, 但都没啥可说的.
一直到遇到如下这个问题:
Compilation failed: support for \P, \p, and \X has not been compiled
at offset 16 in /***/search-everything.php on line 802该出的正则是:
$postcontent = preg_replace(
'"(?<!\<)(?<!\w)(\pL*'.$term.'\pL*)(?!\w|[^<>]*>)"i'
, '<span class="search-everything-highlight-color"
style="background-color:'.$highlight_color.'">$1</span>'
, $postcontent
);原来是在正则中使用了Unicode Properties \p{}
原因见:http://www.fredsantos.net/index.php?option=com_content&view=article&id=114:unicode-support-on-centos-52-with-php-and-pcre&catid=36:linux&Itemid=85
解决的办法也挺简单, 就是避开使用\p{L}(\pL是简写):
$postcontent = preg_replace(
'"(?<!\<)(?<!\w)([\x{41}-\x{5a}\x{61}-\x{7A}\x{0800}-\x{d7a3}]*'
. $term . '[\x{41}-\x{5a}\x{61}-\x{7A}\x{0800}-\x{d7a3}]*)(?!\w|[^<>]*>)"ui'
, '<span class="search-everything-highlight-color"
style="background-color:'.$highlight_color.'">$1</span>'
, $postcontent
);如上所示, 直接用Unicode的内码值来匹配, 摘取如上正则相关部分Unicode编码值分布说明如下:
\u4e00-\u9fa5 中文(CJK)
\u3130-\u318F 韩文
\uAC00-\uD7A3 韩文
\u0800-\u4e00 日文BTW: GBK的编码值范围如下:
\x00-\xff GBK双字节编码范围
\x20-\x7f ASCII
\xa1-\xff 中文gb2312
\x80-\xff 中文 gbk