[PHP-users 35729] Re: DOMDocument なんとかなったようです

Youichi Iwakiri iwakiri @ ashimoto.org
2012年 6月 6日 (水) 16:43:18 JST


いわきりです。

(2012/06/05 0:36), Yoshi Saito wrote:

> ただし、
> $meta_before = "<meta charset="Shift_JIS"/>";
> $meta_after = "<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">";
> str_replace($meta_before, $meta_after, $html);
> ではいけませんでした。
> これは何がいけなかったのでしょうか?
> str_replace()を使ったのがいけなかったのか、<などの文字を<などにしなかったのが悪かったのか良く分かりませんでした。
> この<meta>を書き換える方法はどのようにすればいいのでしょうか?

$stream =
fopen('http://www.nhk.or.jp/worldwave/abc/popup/abc120423.html', 'r');

$contents = stream_get_contents($stream);
fclose($stream);
$contents = preg_replace("/<meta charset=\"([A-z-_]+)\">/", "<meta
http-equiv=\"Content-Ty
pe\" content=\"text/html; charset=$1\">", $contents);

$doc = new DOMDocument();
$doc->loadHTML($contents); // metaタグを見て、libxml2がShift_JIS -> UTF-
8を行っているので、以降のmb_convert_encodingは不要では?



PHP-users メーリングリストの案内