我有这个结果(使用$ dom-> saveHTML()):
<!DOCTYPE html>
<html>
<head>
<title>Complete!</title>
</head>
<body>
<div id="foo" class="bar baz" data-foo="a" data-bar="b" data-baz="c"></div>
</body>
</html>
但我希望使用缩进属性保存结果:
<!DOCTYPE html>
<html>
<head>
<title>Complete!</title>
</head>
<body>
<div id="foo"
class="bar baz"
data-foo="a"
data-bar="b"
data-baz="c"></div>
</body>
</html>
如何使用native(?)php DOMDocument / DOMNode / DOMElement / somethingelse?
UPD:是的!我真的需要人类可读的特定格式。答案 0 :(得分:0)
他自己回答。也许这将是一个很好的解决方案:
$html = $dom->saveHTML();
$html = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
$html = explode("\n", $html);
foreach ($html as $num => $line) {
if (preg_match('/^(\s*<[a-z]+\s+)([^>]+)(>)(.*)$/s', $line, $m)) {
$offset = ceil(mb_strlen($m[1]) / 4);
$attrs = preg_split('/"\s+/', $m[2]);
$attrs = join("\"\n" . str_repeat(' ', $offset), $attrs);
$html[$num] = $m[1] . $attrs . $m[3] . $m[4];
}
}
$html = join("\n", $html);