Hier werden Tags eingerückt.
Es folgt demnächst ein erläuternder Text dazu.
<?php
function parseTag($s)
{
$arr = array();
if (substr($s,0,1) !== '<'){
$arr['TYPE'] = 'CONTENT';
return $arr;
}
if (substr($s,0,2) == '</'){
$arr['TYPE'] = 'CLOSE';
} elseif (substr($s,-2) == '/>') {
$arr['TYPE'] = 'SELFCLOSE';
} else {
$arr['TYPE'] = 'OPEN';
}
preg_match('#</?(\S+)(.*)>#',$s, $matches);
$arr['TAG'] = strtolower($matches[1]);
return $arr;
}
$html = <<< EOT
<html><body><p>Hallo</p>
<table>
<tr>
<td>A</td>
<td>A1<img src=""/></td><td>A2</td>
</tr>
<tr><td>B</td></tr>
<tr><td>C</td></tr><tr><td>D</td><td> <table>
<tr>
<td>A</td>
<td>A1<img src=""/></td><td>A2</td>
</tr>
<tr><td>B</td></tr>
<tr><td>C</td></tr><tr><td>D</td></tr>
</table>
</td></tr>
</table>
<DIV>huhu</div>
<br/>
</body>
</html>
EOT;
$tokens = preg_split('#(<[^>]+>)#m', $html,-1, PREG_SPLIT_DELIM_CAPTURE);
$tokens = array_map('trim', $tokens);
# $tokens = array_filter($tokens);
$tmp = array();
foreach($tokens as $t){
$t = trim($t);
if (!empty($t)){
$tmp[] = $t;
}
}
$tokens = $tmp;
$nbr = count($tokens);
$stack = array();
$indent = 0;
$IndentChar = " ";
print '<pre style="font-family:monospace;">';
for ($i=0;$i<$nbr;$i++){
$tmp = $tokens[$i];
$info = parseTag($tmp);
if ($info['TYPE'] == 'OPEN' || $info['TYPE'] == 'SELFCLOSE'){
array_push($stack, $info['TAG']);
$indent++;
if ($last_token == 'OPEN'){
echo '<br>';
}
}
if ($info['TYPE'] == 'OPEN'){
print str_repeat($IndentChar, $indent);
}
if ($info['TYPE'] == 'CLOSE' && $last_token == 'SELFCLOSE'){
print '<br>';
}
if ($info['TYPE'] == 'CLOSE' && $last_token != 'CONTENT'){
print str_repeat($IndentChar, $indent);
}
if ($info['TYPE'] == 'SELFCLOSE' && $last_token != 'CONTENT'){
print str_repeat($IndentChar, $indent);
}
if ($info['TYPE'] == 'DATA' ){
}
print htmlentities($tmp);
if ($info['TYPE'] == 'CLOSE' || $info['TYPE'] == 'SELFCLOSE'){
$el = array_pop($stack);
if ($el != $info['TAG']){
print_r($info);
die ('TAG mismatch '. $el);
}
$indent--;
}
if ($info['TYPE'] == 'CLOSE'){
echo '<br>';
}
$last_token = $info['TYPE'];
}
# print_r($tokens);
?>