3v4l.org

run code in 300+ PHP versions simultaneously
<?php $html = <<<HTML foo <a href='http://test.com'>fóo</a> lórem bár ipsum bar food foo bark. <a>bar</a> not á test HTML; $lookup = [ 'foo' => 'h3', 'bar' => 'h2' ]; libxml_use_internal_errors(true); $dom = new DOMDocument(); $dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $xpath = new DOMXPath($dom); $regexNeedles = []; foreach ($lookup as $word => $tagName) { $regexNeedles[] = preg_quote($word, '~'); } $pattern = '~\b(' . implode('|', $regexNeedles) . ')\b~iu' ; foreach($xpath->query('//*[not(self::a)]/text()') as $textNode) { $newNodes = []; $hasReplacement = false; foreach (preg_split($pattern, $textNode->nodeValue, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $fragment) { $fragmentLower = strtolower($fragment); if (isset($lookup[$fragmentLower])) { $hasReplacement = true; $a = $dom->createElement($lookup[$fragmentLower]); $a->nodeValue = $fragment; $newNodes[] = $a; } else { $newNodes[] = $dom->createTextNode($fragment); } } if ($hasReplacement) { $newFragment = $dom->createDocumentFragment(); foreach ($newNodes as $newNode) { $newFragment->appendChild($newNode); } $textNode->parentNode->replaceChild($newFragment, $textNode); } } echo substr(trim(utf8_decode($dom->saveHTML($dom->documentElement))), 3, -4);
Output for 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Deprecated: Function utf8_decode() is deprecated in /in/XXNkq on line 47 <h3>foo</h3> <a href="http://test.com">fóo</a> lórem bár ipsum <h2>bar</h2> food <h3>foo</h3> bark. <a>bar</a> not á test
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Deprecated: Function utf8_decode() is deprecated in /in/XXNkq on line 47 <h3>foo</h3> <a href="http://test.com">fóo</a> lórem bár ipsum <h2>bar</h2> food <h3>foo</h3> bark. <a>bar</a> not á test
Output for 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.28
<h3>foo</h3> <a href="http://test.com">fóo</a> lórem bár ipsum <h2>bar</h2> food <h3>foo</h3> bark. <a>bar</a> not á test

preferences:
161.5 ms | 403 KiB | 182 Q