У меня есть часть XML, возвращаемая DOMDocument::saveXML()
. Он уже довольно отступом, с двумя пробелами на уровне, например:
<?xml version="1.0"?>
<root>
<error>
<a>eee</a>
<b>sd</b>
</error>
</root>
Как невозможно настроить DOMDocument
(AFAIK) на символ (-ы) отступа, я считал возможным запустить регулярное выражение и изменить отступ заменяя все пары с двумя пробелами на вкладку. Это можно сделать с помощью функции обратного вызова (Demo):
$xml_string = $doc->saveXML();
function callback($m)
{
$spaces = strlen($m[0]);
$tabs = $spaces / 2;
return str_repeat("\t", $tabs);
}
$xml_string = preg_replace_callback('/^(?:[ ]{2})+/um', 'callback', $xml_string);
Теперь мне интересно, возможно ли это сделать без функции обратного вызова (и без e
-modifier (EVAL)). Любые мастера регулярных выражений с идеей?