Facebook应用程序UTF-8 PHP问题

时间:2011-11-24 19:37:31

标签: php facebook

我正在使用PHP开发一个Facebook Iframe应用程序,该应用程序从另一个站点打开RSS源并显示源中的选定项目。 RSS feed如果UTF-8编码,但是当我显示所选项目时,我得到的字符显示为ISO-8859无论如何。

示例:“不要”而不是“不要”

这是我的工作代码:

 $doc = new DOMDocument();
  $doc->load('https://www.otherwebsite.com/rss.php');

  foreach ($doc->getElementsByTagName('item') as $node) {
            $title = $node->getElementsByTagName('title')->item(0)->nodeValue;
            if ($title == $chinese->getAnimal()){
              $horoscope = $node->getElementsByTagName('description')->item(0)->nodeValue;
           }      
  }    
  echo '

Your horoscope : ' . $horoscope . '

'; }

我使用的是PHP 5.2版。任何问题或建议都会被感激,我仍在学习PHP,所以我可能错过了一些对技能更高的从业者来说显而易见的事情。

3 个答案:

答案 0 :(得分:1)

尝试

echo 'Your horoscope : ' . utf8_encode($horoscope);

然而,这仅适用于ISO-8859-1。

或者尝试

$in_encoding = "ISO-8859-1"; //replace with the encoding of the RSS feed
echo 'Your horoscope : ' . iconv($in_encoding, "UTF-8", $horoscope);

答案 1 :(得分:1)

确保您的网页上有一个适当的html标题和UTF-8元数据,例如:

<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1//EN' 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='eng' lang='en'>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<title>WAMP</title>
<meta name='Description' content='Website Under Construction' />
</head>
<body>
CONTENT
</body>
</html>

也可以在回显之前尝试将字符串转换为UTF-8。

$horoscope = utf8_encode("Your Horoscope : $horoscope");
echo $horoscope;

答案 2 :(得分:0)

header('Content-Type: text/html; charset=iso-8859-1')

尝试将其添加到php文件的最顶层。