<?php
function stripTagFormatting(DOMText $textNode) {
$css = [];
$existingStyles = '';
$currentNode = $textNode->parentNode;
do {
switch (strtolower($currentNode->tagName)) {
case 'font':
if ($currentNode->hasAttribute('color') && !isset($css['color'])) {
$css['color'] = $currentNode->getAttribute('color');
}
if ($currentNode->hasAttribute('size') && !isset($css['font-size'])) {
$css['font-size'] = $currentNode->getAttribute('size') . 'px';
}
if ($currentNode->hasAttribute('face') && !isset($css['font-family'])) {
$css['font-family'] = $currentNode->getAttribute('face');
}
break;
case 'b':
if (!isset($css['font-weight'])) {
$css['font-weight'] = 'bold';
}
break;
case 'i':
if (!isset($css['font-style'])) {
$css['font-style'] = 'italic';
}
break;
default:
if ($currentNode->hasAttribute('style')) {
$existingStyles = $currentNode->getAttribute('style');
}
break 2;
}
} while($currentNode->parentNode->childNodes->length == 1 && $currentNode = $currentNode->parentNode);
var_dump($currentNode->tagName, $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) {
stripTagFormatting($node);
}
var_dump($dom->saveHTML());
- Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 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(4) "font"
array(3) {
["font-size"]=>
string(4) "23px"
["font-weight"]=>
string(4) "bold"
["color"]=>
string(7) "#ff00ff"
}
string(4) "font"
array(3) {
["font-size"]=>
string(4) "23px"
["font-weight"]=>
string(4) "bold"
["color"]=>
string(7) "#ff00ff"
}
string(404) "<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><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></body></html>
"
- Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
- Parse error: syntax error, unexpected '[' in /in/LUMED on line 4
Process exited with code 255. - Output for 5.0.0 - 5.0.5
- Parse error: parse error, unexpected '[' in /in/LUMED on line 4
Process exited with code 255. - Output for 4.4.2 - 4.4.9
- Parse error: syntax error, unexpected T_STRING, expecting ')' in /in/LUMED on line 3
Process exited with code 255. - Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
- Parse error: parse error, unexpected T_STRING, expecting ')' in /in/LUMED on line 3
Process exited with code 255. - Output for 4.3.2 - 4.3.4
- Parse error: parse error, expecting `')'' in /in/LUMED on line 3
Process exited with code 255.
preferences:
282.95 ms | 401 KiB | 459 Q