php output xml produces parse error “’”

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

    You take it the wrong way - don't look for a parser which doesn't give you errors. Instead try to have a well-formed xml.

    How did you get from the user? If he literally typed it in, you are not processing the input correctly - for example you should escape & to &. If it is you who put the entity there (perhaps in place of some apostrophe), either define it in DTD () or write it using a numeric notation (), because almost every of the named entities are a part of HTML. XML defines only a few basic ones, as Gumbo pointed out.

    EDIT based on additions to the question:

    • In #1, you escape the content in the way that if user types in ]]> <°)))><, you have a problem.
    • In #2, you are doing the encoding and decoding which result in the original value of the $content. the decoding should not be necessary (if you don't expect users to post values like & which should be interpreted like &).

    If you use htmlspecialchars() with ENT_QUOTES, it should be ok, but see how Drupal does it.

提交回复
热议问题