开发者

utf8_decode error with ampersand (&)

开发者 https://www.devze.com 2023-02-03 09:07 出处:网络
I have been building an API which returns XML code. For example, the API will return: <message>hello, this is a message &amp; it used htmlsepcialchars</message>

I have been building an API which returns XML code. For example, the API will return:

<message>hello, this is a message &amp; it used htmlsepcialchars</message>

I am using file_get_contents() and simplexml_load_string() to load the returned information into an array.

I then used utf8_decode() to convert the message into a friendly charset on the receiving website. However, I receive the following message which pin points to the ampersand (&) in the message:

Warning: main() [function.main]: unterminated entity reference 

Due to the ampersand, the rest of the message is truncated. Any ideas how to get around this p开发者_如何学编程roblem?


it seems your ampersand is not encoded so either double decode it or put it in CDATA field.

Usually special characters like that should be put in CDATA fields. it saves the trouble of having to encode. php DOM creation functions have what you need.


$doc->createCDATASection

and of course :

http://www.w3schools.com/Xml/xml_cdata.asp

and http://fr.php.net/manual/en/function.domdocument-create-cdata-section.php


Well, simpleXML strings must be utf8 encoded, and values entities should be escaped. Is utf_decode() really necessary in your code ?


Found my answer, user error. I was basically escaping again somewhere in my code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号