$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