Bsports必一体育

Web Design
Mobile Internet
Brand Design
Innovative
News
Encyclopedias

smarty如何完美兼容php5.5和preg_replace_callback如何替换preg_replace

Date:2014-05-29 Aource:😘必一运动·(B-sports)官方网站 中国 Type:网站百科
Word Size:small  medium  big

现在PHP5.5版本出来了,我装了一下改动还真不小,咱们用的smarty居然不兼容了,一运行出现以下错误:
preg_replace():
 

意思是说用preg_replace_callback来代替preg_replace里的/e参数,可能是因为安全性的考虑,在php5.5中把这个参数取消了,但是这个preg_replace_callback函数用起来不顺手,我就把那个Smarty_Compiler.class.php文件里的第270行里的preg_replace($search.'e'里的e去掉了,果然好了,可是问题又出来了,这个问题出来的几率不多,就是无法在smarty模板里写入php标签代码:{php}{/php}。

没办法还是要把这个preg_replace()用preg_replace_callback()函数来替换,具体替换方法如下:

在Smarty_Compiler这个类里加入一个方法:

function callback_source($matches){
return "".$this->_quote_replace($this->left_delimiter)."php".str_repeat("n",substr_count("","n"))."".$this->_quote_replace($this->right_delimiter)."";
}

然后在第270行左右找到:
$source_content = preg_replace($search.'e', "'"
. $this->_quote_replace($this->left_delimiter) . 'php'
. "' . str_repeat("n", substr_count('\0', "n")) .'"
. $this->_quote_replace($this->right_delimiter)
. "'"
, $source_content);

替换成:

$source_content = preg_replace_callback($search,
array("self","callback_source")
, $source_content);

即可,这样就可以完美兼容php5.5了。

本文发布于 网站制作公司



Please contact our consultant

+86 10-60259772

Please provide your contact number. The project manager of shangpin China will contact you as soon as possible.

⚡九游体育(NineGame Sports)官方网站 zoty中欧体育kok官网@zoty体育 zoty中欧体育-平台登录 台北外送茶 EMC易倍体育官方 外送茶