simplexmlを使ってxmlをどうにかする


今回使おうとしたxmlのencodingがx-sjis-cp932でした。
そうすると、simplexml_load_stringすると「そんなエンコード知らねぇよ」と言われます。
なので、以下のようにすると良いです。

$file = "./tmp.xml"
if(file_exists($file)){
 $str = file_get_contents($file); // xmlを読み込み
 $buf = mb_convert_encoding($str,"UTF-8"); // UTF-8エンコード変更
 $buf2 = str_replace("x-sjis-cp932","UTF-8",$buf); // ファイル内のx-sjis-cp932をUTF-8にリプレイス
 $xml = simplexml_load_string($buf2); // 読み込み
}