How to skip invalid characters in XML file using PHP

后端 未结 7 1759
再見小時候
再見小時候 2020-12-01 05:51

I\'m trying to parse an XML file using PHP, but I get an error message:

parser error : Char 0x0 out of allowed range in

I think it\'

7条回答
  •  無奈伤痛
    2020-12-01 06:17

    Do you have control over the XML? If so, ensure the data is enclosed in .. ]]> blocks.

    And you also need to clear the invalid characters:

    /**
     * Removes invalid XML
     *
     * @access public
     * @param string $value
     * @return string
     */
    function stripInvalidXml($value)
    {
        $ret = "";
        $current;
        if (empty($value)) 
        {
            return $ret;
        }
    
        $length = strlen($value);
        for ($i=0; $i < $length; $i++)
        {
            $current = ord($value{$i});
            if (($current == 0x9) ||
                ($current == 0xA) ||
                ($current == 0xD) ||
                (($current >= 0x20) && ($current <= 0xD7FF)) ||
                (($current >= 0xE000) && ($current <= 0xFFFD)) ||
                (($current >= 0x10000) && ($current <= 0x10FFFF)))
            {
                $ret .= chr($current);
            }
            else
            {
                $ret .= " ";
            }
        }
        return $ret;
    }
    

提交回复
热议问题