I need to process a DOM and remove all hyperlinks to a particular site while retaining the underlying text. Thus, something ling <a href="abc.com">text</a>
changes into text
. Taking cue from this thread, I wrote this:
$as = $dom->getElementsByTagName('a');
for ($i = 0; $i < $as->length; $i++) {
$node = $as->item($i);
$link_href = $node->getAttribute('href');
if (strpos($link_href,'offendinglink.com') !== false) {
$cl = $node->getAttribute('class');
$text = new DomText($node->nodeValue);
$node->parentNode->insertBefore($text, $node);
$node->parentNode->removeChild($node);
$i--;
}
}
This works fine except that I also need to retain the class attributed to the offending <a>
tag and maybe turn it into a <div>
or a <span>
. Thus, I need this:
<a href="www.offendinglink.com" target="_blank" class="nice" id="nicer">text</a>
to turn into this:
<div class="nice">text</div>
How do I access the new element after it's been added (like in my code snippet)?