3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function isHTMLTextNode(DOMNode $node) { return $node instanceof DOMText || ($node instanceof DOMElement && strtolower($node->tagName) === 'br'); } function hasMultipleNonTextChildren(DOMElement $element) { $foundOne = false; foreach ($element->childNodes as $child) { if (!isHTMLTextNode($child)) { if ($foundOne) { return true; } $foundOne = true; } } return false; } function setStylesOnElement(DOMElement $element, array $css) { $styles = ltrim(rtrim($element->getAttribute('style'), ';') . '; ', ';'); foreach ($css as $prop => $val) { $styles .= $prop . ': ' . $val . '; '; } $element->setAttribute('style', trim($styles)); } function getCSSEquivs(DOMElement $element) { switch (strtolower($element->tagName)) { case 'font': $css = []; if ($element->hasAttribute('color')) { $css['color'] = $element->getAttribute('color'); } if ($element->hasAttribute('size')) { $css['font-size'] = $element->getAttribute('size') . 'px'; } if ($element->hasAttribute('face')) { $css['font-family'] = $element->getAttribute('face'); } return $css; case 'b': return ['font-weight' => 'bold']; case 'i': return ['font-style' => 'italic']; } return null; } function pullChildrenUp(DOMElement $element) { $children = []; $current = $element->firstChild; do { $next = $current->nextSibling; $children[] = $element->removeChild($current); } while ($current = $next); $parent = $element->parentNode; $before = $element->nextSibling; $parent->replaceChild(array_shift($children), $element); while ($children) { $parent->insertBefore(array_shift($children), $before); } } function pullChildrenDownIntoSpan($element, array $css = []) { $span = $element->ownerDocument->createElement('span'); $span->appendChild($element->replaceChild($span, $element->firstChild)); while ($span->nextSibling) { $span->appendChild($element->removeChild($span->nextSibling)); } return $span; } function stripFormattingTags(DOMText $textNode) { $css = []; $currentNode = $textNode->parentNode; while ($currentNode && !hasMultipleNonTextChildren($currentNode)) { if (null === $equivs = getCSSEquivs($currentNode)) { break; } $css = array_merge($equivs, $css); $parentNode = $currentNode->parentNode; pullChildrenUp($currentNode); $currentNode = $parentNode; } if ($currentNode->tagName !== 'span') { $currentNode = pullChildrenDownIntoSpan($currentNode); } if ($css) { setStylesOnElement($currentNode, $css); } } $html = '<FONT FACE="League" SIZE="15" COLOR="#000000"><font COLOR="#ff00ff" SIZE="18"><b><font SIZE="23"><font SIZE="27"><font SIZE="23"><font SIZE="18"><font SIZE="23">Moet Flip make-up dragen? <br />Stem mee in de poll!!</font></font></font></font></font></b></font><br /></FONT>'; $dom = new DOMDOcument(); $dom->loadHTML($html); $xpath = new DOMXPath($dom); $nodes = $xpath->evaluate("/html/body//text()"); foreach ($nodes as $node) { stripFormattingTags($node); } var_dump($dom->saveHTML());
Output for 5.4.0 - 5.6.28, hhvm-3.12.0, 7.0.0 - 7.1.0
string(284) "<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html><body><span style="color: #ff00ff; font-size: 23px; font-family: League; font-weight: bold;">Moet Flip make-up dragen? <br>Stem mee in de poll!!<br></span></body></html> "
Output for hhvm-3.10.0

Process exited with code 153.
Output for 5.3.7 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/WBptu on line 40
Process exited with code 255.