3v4l.org

run code in 300+ PHP versions simultaneously
<?php $content = <<< 'HTML' This is an outside Example <p href="https://example.com"> This is a para Example</p> test <markup class="m"> this is a markup example</markup> another example <p>example</p> HTML; // Initialize a DOM object $dom = new DOMDocument(); @$dom->loadHTML("<div>$content</div>"); // Use an HTML element tag as our HTML container // @hakre [https://stackoverflow.com/a/29499718/1020526] $wrapper = $dom->getElementsByTagName('div')->item(0); // Remove wrapper $wrapper = $wrapper->parentNode->removeChild($wrapper); // Remove all nodes of $dom object while ($dom->firstChild) { $dom->removeChild($dom->firstChild); } // Append all $wrapper object nodes to $dom while ($wrapper->firstChild) { $dom->appendChild($wrapper->firstChild); } $dox = new DOMXPath($dom); // Query all elements in addition to text nodes $query = $dox->query('/* | /text()'); // Iterate through all nodes foreach ($query as $node) { // If it's not an HTML element if ($node->nodeType != XML_ELEMENT_NODE) { // Replace desired word / content $newContent = preg_replace('~(example)~i', '<markup>$1</markup>', $node->wholeText); // We can't insert HTML directly into our node // so we need to create a document fragment $newNode = $dom->createDocumentFragment(); $newNode->appendXML($newContent); // Replace new content with old one $node->parentNode->replaceChild($newNode, $node); } } // Save modifications echo $dom->saveHTML($dom);
Output for 7.0.0 - 7.0.25, 7.1.0 - 7.1.20, 7.2.6 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0 - 8.5.1
This is an outside <markup>Example</markup> <p href="https://example.com"> This is a para Example</p> test <markup class="m"> this is a markup example</markup> another <markup>example</markup> <p>example</p>
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.36' not found (required by /usr/lib/libm.so.6) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_ABI_DT_RELR' not found (required by /usr/lib/libm.so.6) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /usr/lib/libm.so.6) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.35' not found (required by /usr/lib/libm.so.6)
Process exited with code 1.

preferences:
185.57 ms | 408 KiB | 5 Q