无为清净楼资源网 Design By www.qnjia.com

在编写PHP模板引擎工具类时,以前常用的一个正则替换函数为 preg_replace(),加上正则修饰符 /e,就能够执行强大的回调函数,实现模板引擎编译(其实就是字符串替换)。

详情介绍参考博文:PHP函数preg_replace() 正则替换所有符合条件的字符串

应用举例如下:
复制代码 代码如下:
<"/\<\!\-\-\{if\s+(.+", "\$this->ifTag('\\1')", $template);

  return $template;
 }

 /**
  * if 标签
  */
 protected function ifTag($str) {

  //$str = stripslashes($str); // 去反转义

  return '<"password"]}-->zzz';

$tplComplier = new Template();

$template = $tplComplier->compile($template);

echo $template;

"codetitle">复制代码 代码如下:
xxx<"password\"]) { "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://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);
"codetitle">复制代码 代码如下:
<"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";
"codetitle">复制代码 代码如下:
<"April fools day is 04/01/2002\n";
$text.= "Last christmas was 12/24/2001\n";
// 回调函数
function next_year($matches) {
  // 通常: $matches[0]是完成的匹配
  // $matches[1]是第一个捕获子组的匹配
  // 以此类推
  return $matches[1].($matches[2]+1);
}
echo preg_replace_callback(
            "|(\d{2}/\d{2}/)(\d{4})|",
            "next_year",
            $text);

"codetitle">复制代码 代码如下:
<"/\{([0-9a-zA-Z\- ]+)\:([0-9a-zA-Z\- ]+):";
    return preg_replace_callback($reg, array($this, 'replace'), $text);
  }
 
  private function replace($matches){
    if (method_exists($this, $matches[1])){
      return @$this->$matches[1]($matches[2]);    
    }
  } 
}
"self::replace" :
注意,也可以是 array($this, 'replace')。
复制代码 代码如下:
<"/\{([0-9a-zA-Z\- ]+)\:([0-9a-zA-Z\- ]+):";
    return preg_replace_callback($reg, "self::replace", $text);
  }
 
  private function replace($matches){
    if (method_exists($this, $matches[1])){
      return @$this->$matches[1]($matches[2]);    
    }
  } 
}
"codetitle">复制代码 代码如下:
<"/\<\!\-\-\{if\s+(.+", array($this, 'ifTag'), $template);

  return $template;
 }

 /**
  * if 标签
  */
 protected function ifTag($matches) {
  return '<"password"]}-->zzz';

$tplComplier = new Template();

$template = $tplComplier->compile($template);

echo $template;

"codetitle">复制代码 代码如下:
xxx<"password"]) { "color: #800000">PS:关于正则,本站还提供了2款非常简便实用的正则表达式工具供大家使用:

JavaScript正则表达式在线测试工具:
http://tools.jb51.net/regex/javascript

正则表达式在线生成工具:
http://tools.jb51.net/regex/create_reg

标签:
PHP,正则替换函数,preg_replace,preg_replace_callback

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com