params – {Object.} – Map of strings or objects which will be serialized with the paramSerializer and appended as GET parameters.
$content = <<<HTML
<p>Lorem <a href="#">ipsum <span>span text</span> <div>div test <p>p text</p></div></a> dolor sit amet, consectetur adipisicing elit. Explicabo porro illum nesciunt. <a href="#">Voluptatem</a>, blanditiis esse, quod <a href="#">expedita</a> repellendus consequuntur dolore pariatur dicta tempora facere sint labore at magnam, rem assumenda.</p>
<p>Commodi, expedita? <a href="#">Accusantium reiciendis</a> in voluptatem veniam ullam! Et, porro architecto in dicta maxime blanditiis iusto, totam quis rerum voluptas numquam vero doloremque! Itaque impedit eligendi dignissimos, et <a href="#">veritatis eius</a>.</p>
HTML;
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->loadHTML($content);
$xpath = new DOMXPath($dom);
foreach($xpath->query('//a') as $element) {
if ($element->nodeName === 'a' && $element->hasAttribute('href')) {
$href = $element->getAttribute('href');
$newElement = $dom->createTextNode('<a href="' . $href . '">' . $element->textContent . '</a>');
$element->parentNode->replaceChild($newElement, $element);
}
}
echo $dom->saveHTML();