3v4l.org

run code in 300+ PHP versions simultaneously
<?php function applyHLToSiblings(DOMDocument $D){ $words[0] = "cart"; /* $words[1] = "car"; $words[2] = "cartel"; $words[3] = "art"; $words[4] = "drug"; $words[5] = "drugs"; $words[6] = "rug"; $words[7] = "rugs"; */ $mt = function($content) use($D){ return $D->createTextNode($content); }; $ms = function($content = null, $name) use($D){ $span = $D->createElement("span"); $span->setAttribute("class", "word$name"); $content === null ?: $span->appendChild($content); return $span; }; $ps = function($siblingsContainer, $searchString, $name) use($D, $ms, $mt){ foreach($siblingsContainer->childNodes as $node){ if($node instanceof DOMText){ if(mb_strlen($node->textContent, "UTF-8") >= mb_strlen($searchString)){ $o = mb_strpos($node->textContent, $searchString); if($o !== false){ $l = mb_strlen($searchString, "UTF-8"); $before = mb_substr($node->textContent, 0, $o); $wrap = mb_substr($node->textContent, $o, $l); $after = mb_substr($node->textContent, $o + $l); $after ? $node->parentNode->insertBefore($mt($after) , $node->nextSibling) : null; $node->parentNode->insertBefore($ms($mt($wrap), $name) , $node->nextSibling); $before ? $node->parentNode->insertBefore($mt($before) , $node->nextSibling) : null; $node->parentNode->removeChild($node); } } } } }; foreach($words as $name => $word) $ps($D->documentElement, $word, $name); } $x = new DOMDocument(); $x->loadXML("<data>drugs cartel</data>"); applyHLToSiblings($x); var_dump($x->saveXML());
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Deprecated: Optional parameter $content declared before required parameter $name is implicitly treated as a required parameter in /in/OOE7Q on line 20 string(75) "<?xml version="1.0"?> <data>drugs <span class="word0">cart</span>el</data> "
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: Optional parameter $content declared before required parameter $name is implicitly treated as a required parameter in /in/OOE7Q on line 20 string(75) "<?xml version="1.0"?> <data>drugs <span class="word0">cart</span>el</data> "
Output for 8.0.0 - 8.0.30
Deprecated: Required parameter $name follows optional parameter $content in /in/OOE7Q on line 20 string(75) "<?xml version="1.0"?> <data>drugs <span class="word0">cart</span>el</data> "
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33
string(75) "<?xml version="1.0"?> <data>drugs <span class="word0">cart</span>el</data> "

preferences:
143.08 ms | 403 KiB | 183 Q