@ 2014-07-09T13:11:18Z <?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());
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 5.4.0 - 5.4.45 , 5.5.24 - 5.5.35 , 5.6.7 - 5.6.28 , 7.0.0 - 7.0.20 , 7.1.0 - 7.1.10 , 7.2.0 - 7.2.33 , 7.3.12 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.6 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 5.3.7 - 5.3.29 Parse error: syntax error, unexpected '[' in /in/WBptu on line 40
Process exited with code 255 . preferences:dark mode live preview
194.13 ms | 401 KiB | 252 Q