3v4l.org

run code in 300+ PHP versions simultaneously
<?php $str = 'This is a text with multiple anchor tags. This is the first one: <a href="https://www.link1.com/" title="Link 1">Link 1</a> and this one the second: <a href="https://www.link2.com/" title="Link 2">Link 2</a> after that a lot of other text. And here the 3rd one: <a href="https://www.link3.com/" title="Link 3">Link 3</a> Some other text.'; $dom = new DOMDocument(); // since you have a fragment, wrap it in a <body> $dom->loadHTML("<body>".$str."</body>"); $links = $dom->getElementsByTagName("a"); while($link = $links[0]) { $link->parentNode->insertBefore(new DOMText($link->getAttribute("href")),$link); $link->parentNode->removeChild($link); } $result = $dom->saveHTML($dom->getElementsByTagName("body")[0]); // remove <body>..</body> wrapper $output = substr($result, strlen("<body>"), -strlen("</body>")); echo $output;

preferences:
23.87 ms | 404 KiB | 5 Q