Home >Backend Development >PHP Tutorial >preg_replace_callback函数解决方案

preg_replace_callback函数解决方案

WBOY
WBOYOriginal
2016-06-13 11:50:35923browse

preg_replace_callback函数

$val = preg_replace("/\[([^\[\]]*)\]/eis", "'.'.str_replace('$','\$','\\1')", $val);

这一句 用preg_replace_callback函数怎么改写呢 求指教下 
------解决方案--------------------
$val = preg_replace_callback("/\[([^\[\]]*)\]/eis", function($match){ return str_replace('$','\$',$match)}, $val);

------解决方案--------------------
$val = preg_replace_callback(<br />        '/\[([^\[\]]*)\]/is',<br />        function ($matches) {<br />            return '.'.str_replace('$','\$',$matches[1]);<br />        },<br />        $val<br />    );<br />echo $val;

------解决方案--------------------

引用:
最后一处了 这里中间的代码 include哪一行不清楚怎么改
<br />$pattern = '/<!--\s#BeginLibraryItem\s\"\/(.*?)\"\s-->.*?<!--\s#EndLibraryItem\s-->/s';<br />$replacement = "'{include file='.strtolower('\\1'). '}'";<br />$source = preg_replace_callback($pattern, $replacement, $source);
- -

还是一样的 也还是一字符串啊 还是原来的方式就行了
------解决方案--------------------
$replacement = function ($matches) { return '{include file='.strtolower($matches[1]). '}';};
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn