For example, remove links to here but keep text but leave all other
$html = '...I can haz HTML?...';
$whitelist = array('herpyderp.com', 'google.com');
$dom = new DomDocument();
$dom->loadHtml($html);
$links = $dom->getELementsByTagName('a');
foreach($links as $link){
$host = parse_url($link->getAttribute('href'), PHP_URL_HOST);
if($host && !in_array($host, $whitelist)){
// create a text node with the contents of the blacklisted link
$text = new DomText($link->nodeValue);
// insert it before the link
$link->parentNode->insertBefore($text, $link);
// and remove the link
$link->parentNode->removeChild($link);
}
}
// remove wrapping tags added by the parser
$dom->removeChild($dom->firstChild);
$dom->replaceChild($dom->firstChild->firstChild->firstChild, $dom->firstChild);
$html = $dom->saveHtml();
For those scared to use DomDocument instead of preg_replace
for performance reasons, I did a quick test between this and the code linked in the Q (the one that completely removes the links) => DomDocument is only ~4 times slower.