LoginSignup
11
7

More than 5 years have passed since last update.

RSS フィード (XML) のエラー「PCDATA invalid Char value XX」対処方法 (PHP編)

Last updated at Posted at 2015-10-28

ブログ記事 & 最新情報

最新情報があります。こちらからご覧いただけます。
http://ja.katzueno.com/2015/10/3517/

本文

直感的な操作が売りの CMS である「concrete5」の古いバージョンで RSS フィードがうまく表示されないという問題がありました。

原因は UTF8 だけど、XML では使えない文字列が存在していたので、RSS Feed リーダーでエラーが出ていたのでした。

テキストも UTF8 にきちんとエンコーディングして、文字列も
<?xml version="1.0" encoding="UTF-8"?>
と指定しているのに

Chrome で
PCDATA invalid Char value XX. Below is a rendering of the page up to the first error.

というエラーが表示され、正常に XML が表示されなくなっていました。

これは、XML ではサポートしていない文字列があるからです。

なので、それらの 文字列を取り除く簡単な PHP 処理がこれです。

function utf8_for_xml($string)
{
    return preg_replace ('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $string);
}

参考 & コード引用
http://stackoverflow.com/questions/12229572/php-generated-xml-shows-invalid-char-value-27-message

11
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
7