PHP Universal Feed Generatorでタイトルの文字化け対策
- http://www.ajaxray.com/blog/2008/03/08/php-universal-feed-generator-supports-rss-10-rss-20-and-atom/
- PHPでATOM、RSS1/2フォーマットのフィードが超簡単に吐き出せる「FeedWriter.php」:phpspot開発日誌
PHPSPOT 開発日誌で紹介されていた RSS 出力ライブラリの PHP Universal Feed Generator ですが、そのまま使用すると日本語のタイトルが文字化けして、XML として well-formed になりません。
これは、タイトルの出力部分で htmlentities 関数が使用されているためです。
FeedWriter.php 298行目
<?php { $nodeText .= (in_array($tagName, $this->CDATAEncoding))? $tagContent : htmlentities($tagContent); } ?>
日本語の RSS を出力する場合は、htmlentities の第3引数で文字コードを指定します。
<?php { $nodeText .= (in_array($tagName, $this->CDATAEncoding))? $tagContent : htmlentities($tagContent, ENT_QUOTES, 'UTF-8' ); } ?>