简体   繁体   中英

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. The RSS feed if UTF-8 encoded, however when I display the selected item I get the characters displayed as ISO-8859 whatever.

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 '

: ' . $horoscope . '

'; }

I am using version 5.2 of PHP. 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.

Try

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

However that will only work for 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:

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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