我不明白这一点更换整个DIV如何与另一在PHP的preg_replace替换一个div如何与其他用了preg_replace正则表达式
$string ='
<div id="myid">this has to be replaced</div>
<p>here is something</p>
<div id="any">any text not to be replaced</div>
';
,如果我做
$string = preg_replace('/<div id=\"myid\">.*<\/div>/','anything',$string);
它不起作用,我不明白为什么?!
因为量词默认为贪婪。看看http://www.regular-expressions.info/repeat.html。更好的是:使用HTML解析器和DOM操作来做你想做的事情。 – 2013-03-08 14:00:10
它适用于我。 – Pitchinnate 2013-03-08 14:03:17