您现在的位置: 365建站网 > 365文章 > PHP正则替换函数preg_replace和preg_replace_callback使用总结

PHP正则替换函数preg_replace和preg_replace_callback使用总结

文章来源:365jz.com     点击数:499    更新时间:2017-08-17 13:02   参与评论

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

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

应用举例如下:

</>code

  1. <?php
  2. /**
  3. * 模板解析类
  4. */
  5. class Template {
  6. public function compile($template) {
  7.      // if逻辑  
  8.   $template = preg_replace("/\<\!\-\-\{if\s+(.+?)\}\-\-\>/e", "\$this->ifTag('\\1')", $template);
  9.      return $template; 
  10. }
  11.      /**   * if 标签   */ 
  12. protected function ifTag($str) {
  13.      //$str = stripslashes($str); // 去反转义
  14.      return '<?php if (' . $str . ') { ?>'; 
  15. }
  16. }
  17. $template = 'xxx<!--{if $user[\'userName\']}-->yyy<!--{if $user["password"]}-->zzz';
  18. $tplComplier = new Template();
  19. $template = $tplComplier->compile($template);
  20. echo $template;
  21. ?>

输出结果为:

xxx<?php if ($user['userName']) { ?>yyy<?php if ($user[\"password\"]) { ?>zzz

 

仔细观察,发现 $user["password"] 中的双引号被转义了,这不是我们想要的结果。

为了能够正常输出,还必须反转义一下,但是,如果字符串中本身含有反转义双引号的话,我们此时反转义,原本的反转义就变成了非反转义了,这个结果又不是我们想要的,所以说这个函数在这方面用的不爽!

后来,发现一个更专业级的 正则替换回调函数 preg_replace_callback()。

php7中,preg_replace()不再支持"\e" (PREG_REPLACE_EVAL),需要使用preg_replace_callback()来代替。

看例子来说吧

$content = preg_replace("/{#(.+?)}/eis", '$lang[\'\\1\']', $content) ;

这是原来的代码,在php7里不能被正确执行。

$content = preg_replace_callback("/{#(.+?)}/is", function($r)use($lang){ return $lang[$r[1]]; }, $content);

这是改过之后的代码

需要注意:

1. preg_replace_callback第二个参数,callback()函数里,如果需要使用外部的变量,可以使用function callback($matchs)use($xxx){}

2. callback() 回调函数里,return 值会替换匹配到的内容

</>code

  1. mixed preg_replace_callback ( mixed pattern, callback callback, mixed subject [, int limit] )
  2. 本函数的行为几乎和 preg_replace() 一样,除了不是提供一个 replacement 参数,而是指定一个 callback 函数。该函数将以目标字符串中的匹配数组作为输入参数,并返回用于替换的字符串。

 

回调函数 callback:

一个回调函数,在每次需要替换时调用,调用时函数得到的参数是从subject 中匹配到的结果。回调函数返回真正参与替换的字符串。这是该回调函数的签名:

</>code

  1. string handler ( array $matches )

像上面所看到的,回调函数通常只有一个参数,且是数组类型。

罗列一些有关preg_replace_callback()函数的实例:

Example #1 preg_replace_callback() 和 匿名函数

 

</>code

  1. <?php
  2. /* 一个unix样式的命令行过滤器,用于将段落开始部分的大写字母转换为小写。 */
  3. $fp = fopen("php://stdin", "r") or die("can't read stdin");
  4. while (!feof($fp)) {
  5.     $line = fgets($fp);
  6.     $line = preg_replace_callback(
  7.         '|<p>\s*\w|',
  8.         function ($matches) {
  9.             return strtolower($matches[0]);
  10.         },
  11.         $line
  12.     );
  13.     echo $line;
  14. }
  15. fclose($fp);
  16. ?>

如果回调函数是个匿名函数,在PHP5.3中,通过关键字use,支持给匿名函数传多个参数,如下所示:

</>code

  1. <?php
  2. $string = "Some numbers: one: 1; two: 2; three: 3 end";
  3. $ten = 10;
  4. $newstring = preg_replace_callback(
  5.     '/(\\d+)/',
  6.     function($match) use ($ten) { return (($match[0] + $ten)); },
  7.     $string
  8.     );
  9. echo $newstring;
  10. #prints "Some numbers: one: 11; two: 12; three: 13 end";
  11. ?>

 

Example #2 preg_replace_callback() 和 一般函数

 

</>code

  1. <?php
  2. // 将文本中的年份增加一年.
  3. $text = "April fools day is 04/01/2002\n";
  4. $text.= "Last christmas was 12/24/2001\n";
  5. // 回调函数
  6. function next_year($matches) {
  7.   // 通常: $matches[0]是完成的匹配
  8.   // $matches[1]是第一个捕获子组的匹配
  9.   // 以此类推
  10.   return $matches[1].($matches[2]+1);
  11. }
  12. echo preg_replace_callback(
  13.             "|(\d{2}/\d{2}/)(\d{4})|",
  14.             "next_year",
  15.             $text);

 

?>

Example #3 preg_replace_callback() 和 类方法

如何在类的内部调用非静态函数?你可以按如下操作: 对于 PHP 5.2,第二个参数 像这样 array($this, 'replace') :

</>code

  1. <?php
  2. class test_preg_callback{
  3.   private function process($text){
  4.     $reg = "/\{([0-9a-zA-Z\- ]+)\:([0-9a-zA-Z\- ]+):?\}/";    
  5. return preg_replace_callback($reg, array($this, 'replace'), $text);   }    
  6. private function replace($matches){     if (method_exists($this, $matches[1])){       return @$this->$matches[1]($matches[2]);         }   }  } ?>

对于 PHP5.3,第二个参数像这样 "self::replace" : 注意,也可以是 array($this, 'replace')。

</>code

  1. <?php
  2. class test_preg_callback{
  3.   private function process($text){     $reg = "/\{([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]);         }   }  } ?>

根据上面所学到的知识点,把模板引擎类改造如下:

 

</>code

  1. <?php
  2. /**
  3.  * 模板解析类
  4.  */
  5. class Template {
  6.  public function compile($template) {

  7.   // if逻辑   $template = preg_replace_callback("/\<\!\-\-\{if\s+(.+?)\}\-\-\>/", array($this, 'ifTag'), $template);

  8.   return $template;  }

  9.  /**   * if 标签   */  protected function ifTag($matches) {   return '<?php if (' . $matches[1] . ') { ?>';  } }

  10. $template = 'xxx<!--{if $user[\'userName\']}-->yyy<!--{if $user["password"]}-->zzz';

  11. $tplComplier = new Template();

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

  13. echo $template;

  14. ?>

输出结果为:

</>code

  1. xxx<?php if ($user['userName']) { ?>yyy<?php if ($user["password"]) { ?>zzz

正是我们想要的结果,双引号没有被反转义!

如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛

发表评论 (499人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
昵称:
最新评论
------分隔线----------------------------

快速入口

· 365软件
· 杰创官网
· 建站工具
· 网站大全

其它栏目

· 建站教程
· 365学习

业务咨询

· 技术支持
· 服务时间:9:00-18:00
365建站网二维码

Powered by 365建站网 RSS地图 HTML地图

copyright © 2013-2024 版权所有 鄂ICP备17013400号