generate functionTue, 08 Nov 2022

get string, convert <a> tags to <div> with onclick on the href attribute

function changeATags($html) { $dom = new DOMDocument; $dom->loadHTML($html); $as = $dom->getElementsByTagName('a'); foreach ($as as $a) { $div = $dom->createElement('div', $a->nodeValue); $div->setAttribute('onclick', $a->getAttribute('href')); $a->parentNode->replaceChild($div, $a); } return $dom->saveHTML(); }

Questions about programming?Chat with your personal AI assistant