PHP 生成的 XML 显示无效的 Char 值 27 消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12229572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP generated XML shows invalid Char value 27 message
提问by Prashant
I am generating XML using PHP library as below:
我正在使用 PHP 库生成 XML,如下所示:
$dom = new DOMDocument("1.0","utf-8");
Doing above results in a page which shows a message on top of the output.
执行上述操作会生成一个页面,该页面在输出顶部显示一条消息。
This page contains the following errors: error on line 16 at column 274505: PCDATA invalid Char value 27 Below is a rendering of the page up to the first error.
此页面包含以下错误: 第 16 行第 274505 列错误:PCDATA 无效字符值 27 下面是页面呈现直到第一个错误。
I have tried rectifying using Tidy library.. used iconv to get the chinese character in UTF-8.
我曾尝试使用 Tidy 库进行纠正。使用 iconv 获取 UTF-8 中的中文字符。
回答by Prashant
A useful function to get rid of that error is suggested on this website. http://www.phpwact.org/php/i18n/charsets#common_problem_areas_with_utf-8
本网站建议使用一个有用的功能来消除该错误。 http://www.phpwact.org/php/i18n/charsets#common_problem_areas_with_utf-8
When you put utf-8 encoded strings in a XML document you should remember that not all utf-8 valid chars are accepted in a XML document http://www.w3.org/TR/REC-xml/#charsets
当您将 utf-8 编码字符串放入 XML 文档时,您应该记住,并非所有 utf-8 有效字符都被 XML 文档接受http://www.w3.org/TR/REC-xml/#charsets
So you should strip away the unwanted chars, else you'll have an XML fatal parsing error such as above
所以你应该去掉不需要的字符,否则你会遇到像上面这样的 XML 致命解析错误
function utf8_for_xml($string)
{
return preg_replace ('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $string);
}
Hope that saves someone else some time..
希望能节省别人一些时间..
回答by Quang Tran
Prashant is absolutely right. You can also strip away invalid characters in Javascript by doing:
Prashant 是完全正确的。您还可以通过执行以下操作去除 Javascript 中的无效字符:
function utf8_for_xml(inputStr) {
return inputStr.replace(/[^\x09\x0A\x0D\x20-\xFF\x85\xA0-\uD7FF\uE000-\uFDCF\uFDE0-\uFFFD]/gm, '');
}

