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.4.1 - 8.4.14
Deprecated: Function utf8_decode() is deprecated since 8.2, visit the php.net documentation for various alternatives 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.2.0 - 8.2.29, 8.3.0 - 8.3.27
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.33
<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:
131.15 ms | 410 KiB | 5 Q