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;
Output for 5.6.3 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.30, 8.2.0 - 8.2.25, 8.3.0 - 8.3.13
This is a text with multiple anchor tags. This is the first one: https://www.link1.com/ and this one the second: https://www.link2.com/ after that a lot of other text. And here the 3rd one: https://www.link3.com/ Some other text.
Output for 5.6.0 - 5.6.2
Fatal error: Cannot use object of type DOMNodeList as array in /in/7Wgqn on line 9
Process exited with code 255.

preferences:
78.02 ms | 407 KiB | 5 Q