preg_replace: 贪心和不贪心一样吗?
preg_replace: Greedy and ungreedy the same?
我的代码是:
$s = '<div style="text-align: justify;">Some text</div>';
$s .= '<div style="text-align: justify;"> </div>';
$s .= '<div style="text-align: justify;">Some more text</div>';
$t1 = preg_replace("/<div(.*)> <\/div>/isU", "REPLACED", $s);
print $t1;
$t2 = preg_replace("/<div(.*)> <\/div>/is", "REPLACED", $s);
print $t2;
思路是:我想去掉所有只有 的div在里面。
问题:无论我将修饰符设置为贪心还是不贪心 (/U),结果总是:
REPLACED<div style="text-align: justify;">Some more text</div>
为什么会这样,我该怎么办?
尝试修改正则表达式:
$t = preg_replace("/<div[^>]*> <\/div>/is", "REPLACED", $s);
我的代码是:
$s = '<div style="text-align: justify;">Some text</div>';
$s .= '<div style="text-align: justify;"> </div>';
$s .= '<div style="text-align: justify;">Some more text</div>';
$t1 = preg_replace("/<div(.*)> <\/div>/isU", "REPLACED", $s);
print $t1;
$t2 = preg_replace("/<div(.*)> <\/div>/is", "REPLACED", $s);
print $t2;
思路是:我想去掉所有只有 的div在里面。
问题:无论我将修饰符设置为贪心还是不贪心 (/U),结果总是:
REPLACED<div style="text-align: justify;">Some more text</div>
为什么会这样,我该怎么办?
尝试修改正则表达式:
$t = preg_replace("/<div[^>]*> <\/div>/is", "REPLACED", $s);