首页 > 其他 > 详细

preg_replace($pattern, $replacement, $content) 修饰符的奇葩作用

时间:2014-03-17 14:39:05      阅读:442      评论:0      收藏:0      [点我收藏+]

$str = "<span>lin</span> == <span>3615</span>";
$pattern = "/<span>(.*)<\/span>/";
$str = preg_replace($pattern, "<span class=‘fuck‘>$1</span>", $str);
print_r($str);

$pattern 中没有问号

结果为:<span class=‘fuck‘>lin</span> == <span>3615</span>

 

$str = "<span>lin</span> == <span>3615</span>";
$pattern = "/<span>(.*?)<\/span>/";
$str = preg_replace($pattern, "<span class=‘fuck‘>$1</span>", $str);
print_r($str);

$pattern 中有问号(相当于防止贪婪匹配)

结果为:<span class=‘fuck‘>lin</span> == <span class=‘fuck‘>3615</span>

 

$str = "<span>lin</span> == <span>3615</span>";
$pattern = "/<span>(.*)<\/span>/U";
$str = preg_replace($pattern, "<span class=‘fuck‘>$1</span>", $str);
print_r($str);

$pattern 中无问号( U 防止贪婪匹配)

结果为:<span class=‘fuck‘>lin</span> == <span class=‘fuck‘>3615</span>

 

 

$str = "<span>lin</span> == <span>3615</span>";
$pattern = "/<span>(.*?)<\/span>/U";
$str = preg_replace($pattern, "<span class=‘fuck‘>$1</span>", $str);
print_r($str);

$pattern 中有问号( U 防止贪婪匹配),相当于失去了U的意义

<span class=‘fuck‘>lin</span> == <span>3615</span>

preg_replace($pattern, $replacement, $content) 修饰符的奇葩作用,布布扣,bubuko.com

preg_replace($pattern, $replacement, $content) 修饰符的奇葩作用

原文:http://www.cnblogs.com/lin3615/p/3602544.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!