PHP generated XML shows invalid Char value 27 message

后端 未结 2 1425
生来不讨喜
生来不讨喜 2020-11-29 23:47

I am generating XML using PHP library as below:

$dom = new DOMDocument(\"1.0\",\"utf-8\");

Doing above results in a page which shows a mess

相关标签:
2条回答
  • 2020-11-30 00:32

    Prashant is absolutely right. You can also strip away invalid characters in Javascript by doing:

    function utf8_for_xml(inputStr) {
      return inputStr.replace(/[^\x09\x0A\x0D\x20-\xFF\x85\xA0-\uD7FF\uE000-\uFDCF\uFDE0-\uFFFD]/gm, '');
    }
    
    0 讨论(0)
  • 2020-11-30 00:37

    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

    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

    So you should strip away the unwanted chars, else you’ll have an XML fatal parsing error such as above

    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..

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