Created
June 23, 2013 11:22
-
-
Save kobake/5844686 to your computer and use it in GitHub Desktop.
DOMDocument::loadHTML が meta の charset を解釈してくれない問題と対策 ref: http://qiita.com/kobake@github/items/3c5d09f9584a8786339d
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$url = "http://qiita.com/"; | |
$body = file_get_contents($url); | |
$dom = new DOMDocument(); | |
@$dom->loadHTML($body); | |
$title = $dom->getElementsByTagName('title')->item(0)->textContent; | |
print "$title\n"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$url = "http://qiita.com/"; | |
$body = file_get_contents($url); | |
$body = str_replace('<meta charset="UTF-8">', '<meta charset="UTF-8"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', $body); | |
$dom = new DOMDocument(); | |
@$dom->loadHTML($body); | |
$title = $dom->getElementsByTagName('title')->item(0)->textContent; | |
print "$title\n"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ php dom_charset.php | |
Qiita [ãã¼ã¿] - ããã°ã©ãã®æè¡æå ±å±æãµã¼ãã¹ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- これがあっても意味がない --> | |
<meta charset="UTF-8" /> | |
<!-- こっちを解釈する --> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ php dom_charset2.php | |
Qiita [キータ] - プログラマの技術情報共有サービス |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html xmlns:og="http://ogp.me/ns#"> | |
<head><script type="text/javascript">var NREUMQ=NREUMQ||[];NREUMQ.push(["mark","firstbyte",new Date().getTime()]);</script> | |
<meta charset="UTF-8"> | |
<title>Qiita [キータ] - プログラマの技術情報共有サービス</title> | |
… |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment