preg_replace替换成preg_replace_callback
$str=preg_replace("/\{link(\s+.+?)\}/ies","\$this->striplink('\\1')",$str);//**$str=p...
$str = preg_replace("/\{link(\s+.+?)\}/ies", "\$this->striplink('\\1')", $str);//**
$str = preg_replace("/\{lang\s+(.+?)\}/ies", "\$this->striplang('\\1')",$str);//**
$str = preg_replace("/\{img\s+(.+?)\}/is", "<?php echo img_path(\\1);?>",$str);
$str = preg_replace("/\{mp:(\w+)(\s+[^}]+)\}/ie", "\$this->mp_tag('\\1','\\2', '\\0')", $str);//**
$str = preg_replace("/\{\/mp\}/ie", "\$this->end_mp_tag()", $str);//**
$str = preg_replace("/\{filter:(\w+)(\s+.+?)\}/ie","\$this->do_filter('\\1','\\2')",$str);//**
$str = preg_replace("/\{thumbimg(\s+.+?)\}/ies","\$this->makethumbpath('\\1')",$str);//** 展开
$str = preg_replace("/\{lang\s+(.+?)\}/ies", "\$this->striplang('\\1')",$str);//**
$str = preg_replace("/\{img\s+(.+?)\}/is", "<?php echo img_path(\\1);?>",$str);
$str = preg_replace("/\{mp:(\w+)(\s+[^}]+)\}/ie", "\$this->mp_tag('\\1','\\2', '\\0')", $str);//**
$str = preg_replace("/\{\/mp\}/ie", "\$this->end_mp_tag()", $str);//**
$str = preg_replace("/\{filter:(\w+)(\s+.+?)\}/ie","\$this->do_filter('\\1','\\2')",$str);//**
$str = preg_replace("/\{thumbimg(\s+.+?)\}/ies","\$this->makethumbpath('\\1')",$str);//** 展开
1个回答
展开全部
替换成preg_replace_callback的语法如下,注意其中的/e修饰符
<?php
function getFileString($matches) {
return '{include file=' . strtolower($matches[1]) . '}';
}
// for test
$source = '<!-- #BeginLibraryItem "/library/recommend_promotion.lbi" --><!-- #EndLibraryItem -->';
//$pattern = '/<!--\s#BeginLibraryItem\s\"\/(.*?)\"\s-->.*?<!--\s#EndLibraryItem\s-->/se';
$pattern = '/<!--\s#BeginLibraryItem\s\"\/(.*?)\"\s-->.*?<!--\s#EndLibraryItem\s-->/s';
$replacement = '"{include file=' . strtolower('\\1') . '}"';
//$source = preg_replace($pattern, $replacement, $source);
$source = preg_replace_callback($pattern, "getFileString", $source);
print $source;
<?php
function getFileString($matches) {
return '{include file=' . strtolower($matches[1]) . '}';
}
// for test
$source = '<!-- #BeginLibraryItem "/library/recommend_promotion.lbi" --><!-- #EndLibraryItem -->';
//$pattern = '/<!--\s#BeginLibraryItem\s\"\/(.*?)\"\s-->.*?<!--\s#EndLibraryItem\s-->/se';
$pattern = '/<!--\s#BeginLibraryItem\s\"\/(.*?)\"\s-->.*?<!--\s#EndLibraryItem\s-->/s';
$replacement = '"{include file=' . strtolower('\\1') . '}"';
//$source = preg_replace($pattern, $replacement, $source);
$source = preg_replace_callback($pattern, "getFileString", $source);
print $source;
追问
php5.5以后所有修饰符都弃用了还是部份弃用,改的时候直接将/e、/ies等直接删除吗?一直没搞明白,还有返回函数代入报错间题,在改写preg_replace的时候要结合将返回函数一起改么?麻烦帮我写一条,参考一下,非常感谢。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询