PHP正则替换函数preg_replace和preg_replace_callback使用总结
在编写PHP模板引擎工具类时,以前常用的一个正则替换函数为 preg_replace(),加上正则修饰符 /e,就能够执行强大的回调函数,实现模板引擎编译(其实就是字符串替换)。
详情介绍参考博文:PHP函数preg_replace() 正则替换所有符合条件的字符串
应用举例如下:
<?php
/**
* 模板解析类
*/
class Template {
public function compile($template) {
// if逻辑
$template = preg_replace("/\<\!\-\-\{if\s+(.+?)\}\-\-\>/e", "\$this->ifTag('\\1')", $template);
return $template;
}
/**
* if 标签
*/
protected function ifTag($str) {
//$str = stripslashes($str); // 去反转义
return '<?php if (' . $str . ') { ?>';
}
}
$template = 'xxx<!--{if $user[\'userName\']}-->yyy<!--{if $user["password"]}-->zzz';
$tplComplier = new Template();
$template = $tplComplier->compile($template);
echo $template;
?>
输出结果为:
xxx<?php if ($user['userName']) { ?>yyy<?php if ($user[\"password\"]) { ?>zzz
仔细观察,发现 $user["password"] 中的双引号被转义了,这不是我们想要的结果。
为了能够正常输出,还必须反转义一下,但是,如果字符串中本身含有反转义双引号的话,我们此时反转义,原本的反转义就变成了非反转义了,这个结果又不是我们想要的,所以说这个函数在这方面用的不爽!
后来,发现一个更专业级的 正则替换回调函数 preg_replace_callback()。
mixed preg_replace_callback ( mixed pattern, callback callback, mixed subject [, int limit] )
本函数的行为几乎和 preg_replace() 一样,除了不是提供一个 replacement 参数,而是指定一个 callback 函数。该函数将以目标字符串中的匹配数组作为输入参数,并返回用于替换的字符串。
回调函数 callback:
一个回调函数,在每次需要替换时调用,调用时函数得到的参数是从subject 中匹配到的结果。回调函数返回真正参与替换的字符串。这是该回调函数的签名:
string handler ( array $matches )
像上面所看到的,回调函数通常只有一个参数,且是数组类型。
罗列一些有关preg_replace_callback()函数的实例:
Example #1 preg_replace_callback() 和 匿名函数
<?php
/* 一个unix样式的命令行过滤器,用于将段落开始部分的大写字母转换为小写。 */
$fp = fopen("php://stdin", "r") or die("can't read stdin");
while (!feof($fp)) {
$line = fgets($fp);
$line = preg_replace_callback(
'|<p>\s*\w|',
function ($matches) {
return strtolower($matches[0]);
},
$line
);
echo $line;
}
fclose($fp);
?>
如果回调函数是个匿名函数,在PHP5.3中,通过关键字use,支持给匿名函数传多个参数,如下所示:
<?php
$string = "Some numbers: one: 1; two: 2; three: 3 end";
$ten = 10;
$newstring = preg_replace_callback(
'/(\\d+)/',
function($match) use ($ten) { return (($match[0] + $ten)); },
$string
);
echo $newstring;
#prints "Some numbers: one: 11; two: 12; three: 13 end";
?>
Example #2 preg_replace_callback() 和 一般函数
- 上一篇:11个PHPer必须要了解的编程规范
- 下一篇:php分页函数完整实例代码