XML error at ampersand (&)

后端 未结 5 1853
迷失自我
迷失自我 2020-11-29 07:29

I have a php file which prints an xml based on a MySql db.

I get an error every time at exactly the point where there is an & sign.

Here

相关标签:
5条回答
  • 2020-11-29 07:59
    public function sanitize(string $data) {
        return str_replace('&', '&', $data);
    }
    

    You are right: here is more context - the example is in relation to the ' how to deal with data containing '&' when we pass this data to SimpleXml. Of course there is also other solution to use <![CDATA[some stuff]]>

    0 讨论(0)
  • 2020-11-29 08:02

    & in XML starts an entity. As you haven't defined an entity &WhateverIsAfterThat an error is thrown. You should escape it with &amp;.

    $string = str_replace('&', '&amp;', $string);
    

    How do I escape ampersands in XML

    To escape the other reserved characters:

    function xmlEscape($string) {
        return str_replace(array('&', '<', '>', '\'', '"'), array('&amp;', '&lt;', '&gt;', '&apos;', '&quot;'), $string);
    }
    
    0 讨论(0)
  • 2020-11-29 08:02

    Switch and regex with using xml escape function.

     function XmlEscape(str) {
        if (!str || str.constructor !== String) {
            return "";
        }
    
        return str.replace(/[\"&><]/g, function (match) {
            switch (match) {
            case "\"":
                return "&quot;";
            case "&":
                return "&amp;";
            case "<":
                return "&lt;";
            case ">":
                return "&gt;";
            }
        });
    };
    
    0 讨论(0)
  • 2020-11-29 08:03

    You need to either turn & into its entity &amp;, or wrap the contents in CDATA tags.

    If you choose the entity route, there are additional characters you need to turn into entities:

    >  &gt;
    <  &lt;
    '  &apos;
    "  &quot;
    

    Background: Beware of the ampersand when using XML

    Wikipedia: List of XML character entity references

    0 讨论(0)
  • 2020-11-29 08:13

    $string = htmlspecialchars($string,ENT_XML1);

    is the most universal way to solve all encoding errors (IMHO better that write custom functions + there is no point to solve just &).

    Credit: Put Wrikken's and joshweir's comment as answer to be more visible.

    0 讨论(0)
提交回复
热议问题