简体   繁体   English

Facebook应用程序UTF-8 PHP问题

[英]Facebook Application UTF-8 Issues in PHP

I am developing a Facebook Iframe application using PHP which opens an RSS feed from another site and displays a selected item from the feed. 我正在使用PHP开发一个Facebook Iframe应用程序,该应用程序从另一个站点打开RSS源并显示源中的选定项目。 The RSS feed if UTF-8 encoded, however when I display the selected item I get the characters displayed as ISO-8859 whatever. RSS提要,如果UTF-8编码,但是当我显示所选项目时,我得到的字符显示为ISO-8859无论如何。

Example: "Don’t" instead of "Don't" 示例:“不要”而不是“不要”

This is my working code: 这是我的工作代码:

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

'; }

I am using version 5.2 of PHP. 我使用的是PHP 5.2版。 Any questions or suggestions will be gratefully accepted, I am still learning PHP so I may have missed something obvious to a more skilled practitioner. 任何问题或建议都会被感激,我仍然在学习PHP,所以我可能错过了一些技术娴熟的从业者。

Try 尝试

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

However that will only work for ISO-8859-1. 但是,这仅适用于ISO-8859-1。

Alternatively try 或者尝试

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

Ensure you have a proper html header and meta for UTF-8 on your webpage ie: 确保您的网页上有UTF-8的正确html标题和元数据,即:

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

Also try casting the string to UTF-8 before echoing it. 也请尝试在回显之前将字符串转换为UTF-8。

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM