PHP SimpleXML doesn't preserve line breaks in XML attributes

前端 未结 6 1359
自闭症患者
自闭症患者 2020-12-11 04:31

I have to parse externally provided XML that has attributes with line breaks in them. Using SimpleXML, the line breaks seem to be lost. According to another stackoverflow

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-11 04:58

    The entity for a new line is . I played with your code until I found something that did the trick. It's 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 on the next line, not a < to open a new element.

    This of course would fail if your next line had some text that was wrapped in a line-level element.

提交回复
热议问题