JEdot JEdot - 16 days ago 5
PHP Question

How I can format this line into formatted code using regex

How I can convert this line :

<user_profil><username>root</username><email>root@foo.bar</email></user_profil>


into something formatted like :

<user_profil>
<username>root</username>
<email>root@foo.bar</email>
</user_profil>


using regex.

Also I dont understand why
$doc->saveHTML()
(an instance of DOMDocument) return the result as one line only.

Answer

Actually using regex would be the longer option. Rather use

$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$xml_string = $doc->saveXML();
echo $xml_string;

This should get you the formatted code.

Comments