Here may be what you are looking for (expressed as a
SimpleTest test):
Code: Select all
function testRemoveLinksFollowedByBr() {
$str = "<a href='http://subdomain.domain.com'>This goes</a><br>
<a href='http://subdomain.domain.com'>This will stay</a>
<a href='http://subdomain.domain.com'>This also goes</a><br>";
$expect = "<a href='http://subdomain.domain.com'>This will stay</a>";
$replaced = preg_replace("~<a href='http://subdomain[^']+'>((?!</a>).)+</a><br>~ms", '', $str);
$this->assertEqual($expect, trim($replaced));
}
A couple of pointers that may help you with future regex. If you have a character in your expression, like /, then do not choose it for the delimiter. As you see in mine, I used ~ instead.
Second, an explicity character class with a non-ungreedy match will often perform faster than an ungreedy all chacter match, hence the [^']+. Similarly, you need to stop at
any </a>, not just one which happens to have a <br> behind it, so the match ((?!</a>).)+ grabs anything up to the end of a link, and becuase it always stops there, you do not need it to be ungreedy.
HTH