zl程序教程

您现在的位置是:首页 >  其它

当前栏目

关于thinkhphp3.1中废弃 preg_replace /e 修饰符

关于 replace 修饰符 废弃 preg
2023-09-11 14:18:39 时间

警告:preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead 

 

网上查了下 发现 php5.5版本以上 就废弃了  preg_replace   函数中 /e 这个修饰符

/e 这个修饰符的意思 就是让 正则替换的 时候 替换规则 支持 php 代码 

 

解决方案:只要 把 preg_replace 里面 有 /e 修饰符的代码 修改成  preg_replace _callback  然后重新写下就好了 

 

最简单的 写法:

preg_replace("/([A-Z])/e", "'_' . strtolower('\\1')", $str) 

修改成

preg_replace("/([A-Z])/",'gwyy', $str);  //在类中时写成:['gwyy'] 替换成 array($this,'gwyy')

function gwyy($match) {  

  return  '_'.strtolower($match[1]);  

}  

preg_replace_callback('/([A-Z])/',  function ($matches) { 

   return '_' . strtolower($matches[0]);  

 },  $str)  

 

使用了 类里面的自定义方法:

$that = $this;   //这里 用 $that  代替 $this;  

$patterns       = '/'.$begin.$parseTag.$n1.'\/(\s*?)'.$end.'/is';  

$content=preg_replace_callback($patterns, function($matches) use($tagLib,$tag,$that){

  return $that->parseXmlTag($tagLib, $tag, $matches[1], ''); 

 }, $content);