php preg_replace除外

时间:2011-07-31 14:00:40

标签: php preg-replace

一次测试中有很多段落。

<div class="text">...</div>
<div class="content">...</div>
<div class="aa bb">...</div>
<div class="aa cc">...</div>
<div class="aa dd">...</div>
<div class="aa ee">...</div>
<div class="aa ff">...</div>
...// more type of class="aa .*"

我想删除所有div class="aa",但div class="aa dd"除外,如何完成这项工作?谢谢。

$html = preg_replace('/<div(.*?)class="aa(.*?)">([\s\S]*?)<\/div>/', '', $html);
// this will remove all the div include class 'aa', but I need remain class 'aa dd'

1 个答案:

答案 0 :(得分:2)

echo preg_replace('~<div class="aa (?!dd).+?">.+?</div>~', "", $html);