2012-01-09 11 views
4

サイトのRSSフィードを作成しています。PHP SimpleXMLを生成する際のUTF8エラーRSSフィード

私はSimpleXMLを使用してXML構造を作成しています。私は$ XML-> asXML()を呼び出すと;,それは多くの警告をスロー:

ErrorException [ Warning ]: SimpleXMLElement::asXML() [simplexmlelement.asxml]: string is not in UTF-8 

私は、このエラーが何であるかわかりません。読んでいるデータベーステーブルはutf8_general_ciです。私はそれを修正する代わりに文字列を乱した文字列に対してutf_encodeを実行しようとしました。事前に

//First create the XML root 
$xml = new SimpleXMLElement('<rss version="2.0"></rss>'); 

//Create the Channel 
$channel = $xml->addChild('channel'); 

//Construct the feed parameters 
$channel->addChild('title', 'CGarchitect Feed'); 
$channel->addChild('link', Config::get('base_url')); 
$channel->addChild('description', 'CGarchitect is the leading online community for architectural visualization professionals.'); 
$channel->addChild('pubDate', date("D, d M Y H:i:s T")); 

//Get the feed items 

$nodes = <....snip... > 

foreach ($nodes as $node) 
{ 

    //Parse the title and description 
    $title = htmlentities(strip_tags($node->title)); 
    $description = htmlentities(strip_tags($node->description)); 
    $newItem = $channel->addChild('item'); 
    $newItem->addChild('title', $title); 
    $newItem->addChild('description', $description); 
    $newItem->addChild('pubDate', date("D, d M Y H:i:s T", $node->published_at)); 

} 

header('Content-Type: application/xhtml+xml'); 
echo $xml->asXML(); 

おかげで...

レナード私は

class myNode { 

    public $title="(╯°□°)╯︵ ┻━┻"; 
    public $description="dscr"; 
    public $published_at=0; 

    public function __construct(){ 
     $this->published_at=time(); 
    } 

} 

$nodes = array(new myNode()); 

だけへの呼び出しを削除するとスニペット...あなたの$ノードを交換し、あなたの問題を再現することができました

+0

[MySql接続エンコーディング](http://dev.mysql.com/doc/refman/5.5/en/charset-connection.html)をUTF8に設定しましたか?まあ? – Jon

+0

@Jonはい。 mysql_client_encoding()は 'utf8'を返します –

+0

あなたはデータベースへのUTF-8接続を使用していますか?接続を確立した後、初めてこのクエリを実行します:mysql_query( "SET NAMES 'utf8'"); –

答えて

2

htmlentitiesは正常に動作するように見えました。 (出力は文字エンティティとして正しくエスケープされていました)

+0

これは正常です。 & ....なしで&文字を持つデータベースにいくつかのエントリがあったのでhtmlentitiesを実行していました。 –

+1

あるいは、 htmlentities(strip_tags($ node-> title)、ENT_COMPAT、 'utf-8');それも私のために働く – atxdba

+0

ありがとう。レコードのために、私はEntityのlsquoを定義していないので、htmlentitiesで作業することができませんでした。私はhtmlspecialcharsを使用して切り替え、それは働いた... –

関連する問題