2013-03-19 140 views
2

我正在使用SimplePie(v1.3.1)在網頁上顯示RSS提要。其中一個標題包含歐元(€)符號,並在頁面上顯示爲「」。 當我在他們的網站上使用SimplePie的演示它正確顯示,所以它應該沒有問題。但是我無法讓它工作。PHP - 特殊字符(歐元符號)顯示不正確(RSS提要/ SimplePie)

我已經做了:

  • 搜索上了SimplePie的網站和文檔
  • 搜索上計算器的網站在谷歌
  • 搜索
  • 試了幾十給出的「解決方案」中沒有任何的運氣

我能找到的是,它可能是一個字符編碼問題,據我所知這s應該設置爲UTF-8。 Beneith是我目前的測試代碼,基於SimplePie演示。我已經通過SimplePie的FAQ添加了3個解決方案(請參閱I'm seeing weird characters)。

我在做什麼錯?

<?php 
header('Content-type:text/html; charset=utf-8'); 
// Make sure SimplePie is included. You may need to change this to match the location of autoloader.php 
// For 1.3+: 
require_once('./php/autoloader.php'); 

// We'll process this feed with all of the default options. 
$feed = new SimplePie(); 

// Set the feed to process. 
$feed->set_feed_url(***RSS_FEED_URL_HERE***); 

// Run SimplePie. 
$feed->init(); 

// This makes sure that the content is sent to the browser as text/html and the UTF-8 character set (since we didn't change it). 
$feed->handle_content_type(); 

// Let's begin our XHTML webpage code. The DOCTYPE is supposed to be the very first thing, so we'll keep it on the same line as the closing-PHP tag. 
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US"> 
<head> 
    <title>Sample SimplePie Page</title> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
    <style type="text/css"> 
    /*some css*/ 
    </style> 
</head> 
<body> 
    <div class="header"> 
     <h1><a href="<?php echo $feed->get_permalink(); ?>"><?php echo $feed->get_title(); ?></a></h1> 
     <p><?php echo $feed->get_description(); ?></p> 
    </div> 
    <?php 
    foreach ($feed->get_items() as $item): 
    ?> 
     <div class="item"> 
      <h2><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></h2> 
      <p><?php echo $item->get_description(); ?></p> 
      <p><small>Posted on <?php echo $item->get_date('j F Y | g:i a'); ?></small></p> 
     </div> 
    <?php endforeach; ?> 
</body> 
</html> 

[編輯] 更多信息:

  • 從RSS的XML具有UTF-8編碼(<?xml version="1.0" encoding="UTF-8"?>
  • 使用echo str_replace("€", "&euro;", $item->get_title());作品,但不是很好
+0

只要我沒有更好的答案,我使用Gareth的字符串替換的想法。雖然這很有效(這裏並不令人意外),但我真的希望有一個更穩固的解決方案。 – 4ice 2013-03-19 15:58:28

回答

0

用特殊字符實體替換符號。

&euro; 

如果讓:€

http://www.utexas.edu/learn/html/spchar.html

+0

RSS源來自另一個頁面,所以我無法在此進行任何更改。 – 4ice 2013-03-19 10:24:46

+0

那麼這是沒有好處的O.o運行一個字符串替換從RSS提取的數據。解決這個問題最有可能是一種更簡單的方法,但我無法將它想到我的頭頂。 – 2013-03-19 10:25:44

+0

一個字符串替換可能會工作,但這將是我最後的手段,因爲它不是一個「乾淨的」解決方案。希望別人有其他想法。無論如何,謝謝你的意見。 – 4ice 2013-03-19 10:36:33

0

至於我能看到你的問題是張貼在飼料中的含量是不是UTF-8,即使你的網頁是UTF-8 。

所有你需要做的就是用這個

<?php 
utf8_encode ($feed)// the rss feed from another page 
?> 

希望能夠解決你的問題

+0

不幸的不是。這不會改變任何東西。 – 4ice 2013-03-19 11:00:51

+0

[鏈接](http://stackoverflow.com/questions/6559822/how-to-convert-any-character-encoding-to-utf8-on-php)可能會幫助你 – cooldude5757 2013-03-19 11:16:35

+0

嘗試給你的鏈接給出的可能性,但不改變結果。 RSS feed XML的編碼設置爲 <?xml version =「1.0」encoding =「UTF-8」?> – 4ice 2013-03-19 11:29:19

3

我也有當歐元符號被顯示爲「â,¬」與編碼擺弄後的問題一段時間沒有成功,我只是跟着以下字符串替換:

str_replace(chr(0xE2).chr(0x82).chr(0xAC), '&euro;', $mystring) 

http://www.mauserrifle.nl/php/php-and-replacing-euro-signs/所示。因爲這是一個後端工作,所以我沒有任何性能損失的問題。