3v4l.org

run code in 300+ PHP versions simultaneously
<?php $html = "<p>This paragraph has <b>bold text</b> &amp; <a href='#'>links with <b>bold text</b></a>.</p>"; // load into dom $doc = new DOMDocument(); $doc->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); function replaceOneTag($tag) { static $counter = 0; $counter++; return "<a href='#{$counter}'>{$tag}</a>"; } function replaceTags(array $tags, $node) { // skip links if ($node instanceof DOMElement && $node->hasAttribute("href")) { // skip } // process elements else if ($node instanceof DOMText) { // render back to html, which will deal with entities $oldhtml = $node->ownerDocument->saveHTML($node); // replace tags $newhtml = preg_replace_callback('#\b(' . implode('|', $tags) . ')\b#', function($match) { return replaceOneTag($match[0]); }, $oldhtml); // load the new html string into the document and replace the current DOMText node $newnode = $node->ownerDocument->createDocumentFragment(); $newnode->appendXML($newhtml); $node->parentNode->replaceChild($newnode, $node); } // handle other children else if ($node instanceof DOMNode && $node->hasChildNodes()) { // the child node list updates as the document is modified, which affects the foreach for ($child = $node->firstChild; $child; $child = $nextchild) { // get the next child node in advance $nextchild = $child->nextSibling; replaceTags($tags, $child); } } } print_r(replaceTags(["paragraph", "bold"], $doc)); echo $doc->saveHTML();
Output for git.master, git.master_jit, rfc.property-hooks
<p>This <a href="#1">paragraph</a> has <b><a href="#2">bold</a> text</b> &amp; <a href="#">links with <b>bold text</b></a>.</p>

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
39.02 ms | 405 KiB | 5 Q