php output xml produces parse error “’”

前端 未结 7 1492
时光说笑
时光说笑 2021-01-13 08:24

Is there any function that I can use to parse any string to ensure it won\'t cause xml parsing problems? I have a php script outputting a xml file with content obtained from

7条回答
  •  太阳男子
    2021-01-13 09:21

    I had a similar problem that the data i needed to add to the XML was already being returned by my code as htmlentities() (not in the database like this).

    i used:

    $doc = new DOMDocument('1.0','utf-8');    
    $element = $doc->createElement("content");    
    $element->appendChild($doc->createElement('string', htmlspecialchars(html_entity_decode($string, ENT_QUOTES, 'UTF-8'), ENT_XML1, 'UTF-8')));
    $doc->appendChild($element);
    

    or if it was not already in htmlentities() just the below should work

    $doc = new DOMDocument('1.0','utf-8');
    
    $element = $doc->createElement("content");       
    $element->appendChild($doc->createElement('string', htmlspecialchars($string, ENT_XML1, 'UTF-8')));
    $doc->appendChild($element);
    

    basically using htmlspecialchars with ENT_XML1 should get user imputed data into XML safe data (and works fine for me):

    htmlspecialchars($string, ENT_XML1, 'UTF-8');
    

提交回复
热议问题