DOMDocument appendXML with special characters

前端 未结 5 749
庸人自扰
庸人自扰 2020-12-07 05:25

I am retreiving some html strings from my database and I would like to parse these strings into my DOMDocument. The problem is, that the DOMDocument gives warnings at specia

5条回答
  •  暖寄归人
    2020-12-07 05:43

    There is no   in XML. The only character entities that have an actual name defined (instead of using a numeric reference) are &, <, >, " and '.

    That means you have to use the numeric equivalent of a non-breaking space, which is   or (in hex)  .

    If you are trying to save HTML into an XML container, then save it as text. HTML and XML may look similar but they are very distinct. appendXML() expects well-formed XML as an argument. Use the nodeValue property instead, it will XML-encode your HTML string without any warnings.

    // document fragment is completely unnecessary
    $otherElement->nodeValue = $row['message'];
    

提交回复
热议问题