I have this result (with $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>
But I want save result with indentation of attributes:
<!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>
How make it with native(?) php DOMDocument/DOMNode/DOMElement/somethingelse?
UPD: Yes! I really need that specific formatting for human readable.
Himself answered. Maybe this will be a good solution:
$html = $dom->saveHTML();
$html = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
$html = explode("
", $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("\"
" . str_repeat(' ', $offset), $attrs);
$html[$num] = $m[1] . $attrs . $m[3] . $m[4];
}
}
$html = join("
", $html);