Generating XML document in PHP (escape characters)

前端 未结 10 981
难免孤独
难免孤独 2020-11-27 04:20

I\'m generating an XML document from a PHP script and I need to escape the XML special characters. I know the list of characters that should be escaped; but what is the corr

10条回答
  •  春和景丽
    2020-11-27 05:11

    What about the htmlspecialchars() function?

    htmlspecialchars($input, ENT_QUOTES | ENT_XML1, $encoding);
    

    Note: the ENT_XML1 flag is only available if you have PHP 5.4.0 or higher.

    htmlspecialchars() with these parameters replaces the following characters:

    • & (ampersand) becomes &
    • " (double quote) becomes "
    • ' (single quote) becomes '
    • < (less than) becomes <
    • > (greater than) becomes >

    You can get the translation table by using the get_html_translation_table() function.

提交回复
热议问题