Object for a new line . I played with your code until I found something that did the trick. This is not very elegant, I warn you:
//First remove any indentations: $xml = str_replace(" ","", $xml); $xml = str_replace("\t","", $xml); //Next replace unify all new-lines into unix LF: $xml = str_replace("\r","\n", $xml); $xml = str_replace("\n\n","\n", $xml); //Next replace all new lines with the unicode: $xml = str_replace("\n"," ", $xml); Finally, replace any new line entities between >< with a new line: $xml = str_replace("> <",">\n<", $xml);
The assumption based on your example is that any new lines that occur inside a node or attribute will have more text in the next line, not < to open a new element.
This, of course, will fail if the next line contains text that has been wrapped in a line level element.
Anthony
source share