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($name, $content = null) 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); $before = $ms($name, $wrap); $newSpan->textContent = $wrap; !$before ?: $node->insertBefore($node->nextSibling, $mt($before)); $node->insertBefore($node->nextSibling, $ms($mt($wrap), $name)); !$after ?: $node->insertBefore($node->nextSibling, $mt($after)); } } } } }; 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.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Fatal error: Uncaught TypeError: DOMNode::appendChild(): Argument #1 ($node) must be of type DOMNode, string given in /in/IAqH6:23 Stack trace: #0 /in/IAqH6(23): DOMNode->appendChild('cart') #1 /in/IAqH6(38): {closure}(0, 'cart') #2 /in/IAqH6(52): {closure}(Object(DOMElement), 'cart', 0) #3 /in/IAqH6(60): applyHLToSiblings(Object(DOMDocument)) #4 {main} thrown in /in/IAqH6 on line 23
Process exited with code 255.
Output for 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
Fatal error: Uncaught TypeError: Argument 1 passed to DOMNode::appendChild() must be an instance of DOMNode, string given in /in/IAqH6:23 Stack trace: #0 /in/IAqH6(23): DOMNode->appendChild('cart') #1 /in/IAqH6(38): {closure}(0, 'cart') #2 /in/IAqH6(52): {closure}(Object(DOMElement), 'cart', 0) #3 /in/IAqH6(60): applyHLToSiblings(Object(DOMDocument)) #4 {main} thrown in /in/IAqH6 on line 23
Process exited with code 255.
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
Catchable fatal error: Argument 1 passed to DOMNode::appendChild() must be an instance of DOMNode, string given, called in /in/IAqH6 on line 38 and defined in /in/IAqH6 on line 23
Process exited with code 255.

preferences:
157.53 ms | 402 KiB | 183 Q