php output xml produces parse error “’”

前端 未结 7 1553
时光说笑
时光说笑 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:22

    The problem is that your htmlentities function is doing what it should - generating HTML entities from characters. You're then inserting these into an XML document which doesn't have the HTML entities defined (things like are HTML-specific).

    The easiest way to handle this is keep all input raw (i.e. don't parse with htmlentities), then generate your XML using PHP's XML functions.

    This will ensure that all text is properly encoded, and your XML is well-formed.

    Example:

    $user_input = "...<>&'";
    
    $doc = new DOMDocument('1.0','utf-8');
    
    $element = $doc->createElement("content");
    $element->appendChild($doc->createTextNode($user_input));
    
    $doc->appendChild($element);
    

提交回复
热议问题