What is happening when SimpleXML parses XML with special characters?

我怕爱的太早我们不能终老 提交于 2019-12-02 03:47:37

问题


I am trying to solve this problem I am having with my final output.

The XML feed looks like this...

<?xml version='1.0' encoding='utf-8'?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<item> <title>TRON: Legacy, 2010 - ★★★</title>

I would like to preserve the stars in the final output but my final output looks like this...

TRON: Legacy, 2010 - ★★★

Here is the code I am using in PHP -

$title = $item->title; 
$movieLink = $item->link;

$xml = new SimpleXMLElement($response);
echo "<div class=\"movies\">";
echo "<a href=\"$movieLink\">$title</a>";

I know this might be an encoding issue of some kind and I've tried converting the string using htmlentities but with no luck. Any help will be greatly appreciated.


回答1:


$title will be an UTF-8 encoded string in the output. Signal the browser that your website is in UTF-8 and everything is fine:

header("Content-type:text/html; charset=utf-8");

See as well: Setting the HTTP charset parameter

Only Questionmarks what that means? Probably the quick answer is helpful.



来源:https://stackoverflow.com/questions/10859024/what-is-happening-when-simplexml-parses-xml-with-special-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!