gpt4 book ai didi

php - 如何使用 PHP 跳过 XML 文件中的无效字符

转载 作者:IT王子 更新时间:2023-10-28 23:51:37 25 4
gpt4 key购买 nike

我正在尝试使用 PHP 解析 XML 文件,但收到一条错误消息:

parser error : Char 0x0 out of allowed range in

我认为是因为 XML 的内容,我认为有一个特殊符号“☆”,有什么想法可以修复它吗?

我也明白了:

parser error : Premature end of data in tag item line

什么可能导致该错误?

我正在使用 simplexml_load_file .

更新:

我尝试找到错误行并将其内容粘贴为单个 xml 文件,它可以工作!所以我仍然无法弄清楚是什么导致 xml 文件解析失败。 PS这是一个超过100M的巨大xml文件,会不会导致解析错误?

最佳答案

您对 XML 有控制权吗?如果是这样,请确保数据包含在 <![CDATA[ 中.. ]]> block 。

而且你还需要清除无效字符:

/**
* 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;
}

关于php - 如何使用 PHP 跳过 XML 文件中的无效字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3466035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com