我有一个php应用程序,它有时会失败(取决于我加载的数据),并给出如下错误:
parser error : PCDATA invalid Char value 11
Warning: simplexml_load_file(): ath>/datadrivenbestpractices/Data-driven Best Practices in
Warning: simplexml_load_file(): ^ in 我确信有一些价值观导致了这个问题。我无法控制数据。我尝试过来自Error: "Input is not proper UTF-8, indicate encoding !" using PHP's simplexml_load_string、How to handle invalid unicode with simplexml和How to skip invalid characters in XML file using PHP的解决方案,但它们都没有帮助。
罪魁祸首字符串是:“Data Driven - Best Practices”和“Data-driven Best Practices to招收和留住代表性不足的研究生,2011年5月12日-美国东部时间下午1:30-3:00”(可以是破折号或回车字符)。
我能做什么?我的环境是Windows php测试环境,但实时环境将是LAMP环境--不能接触.ini文件。
谢谢。
发布于 2013-01-23 01:04:27
在解析之前剥离无效字符将是最简单的修复方法:
function utf8_for_xml($string)
{
return preg_replace ('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $string);
}发布于 2013-01-23 02:11:54
不要紧,答案是:How to skip invalid characters in XML file using PHP确实起作用了。下面是我的代码:
stream_filter_register('xmlutf8', 'ValidUTF8XMLFilter');
class ValidUTF8XMLFilter extends php_user_filter
{
protected static $pattern = '/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u';
function filter($in, $out, &$consumed, $closing)
{
while ($bucket = stream_bucket_make_writeable($in)) {
$bucket->data = preg_replace(self::$pattern, '', $bucket->data);
$consumed += $bucket->datalen;
stream_bucket_append($out, $bucket);
}
return PSFS_PASS_ON;
}
}
$doc = simplexml_load_file("php://filter/read=xmlutf8/resource=".$serveraddress.$myparam);https://stackoverflow.com/questions/14463573
复制相似问题