作者woominin (沒事就好)
看板PHP
標題Re: [請益] parser 文字
時間Sun Sep 28 13:46:02 2014
原文恕刪
想請問前輩們
小弟在parser網頁遇到一個新的問題
就是用原本的 simple_parser_dom的工具來parser
http://tour.taitung.gov.tw/zh-tw/Home/Index
會出錯
問題1 : 如何解
再來小弟到處研究了一下
用了另一個 curl
<?php
# Use the Curl extension to query Google and get back a page of results
$url = "
http://tour.taitung.gov.tw/zh-tw/Home/Index";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$html = curl_exec($ch);
curl_close($ch);
# Create a DOM parser object
$dom = new DOMDocument();
# Parse the HTML from Google.
# The @ before the method call suppresses any warnings that
# loadHTML might throw because of invalid HTML in the page.
@$dom->loadHTML($html);
# Iterate over all the <a> tags
foreach($dom->getElementsByTagName('a') as $link) {
# Show the <a href>
echo $link->getAttribute('href');
echo "<br />";
}
foreach($dom->getElementsByTagName('a') as $v) {
echo $v->getAttribute('title');
echo "<br />";
}
?>
用上面的語法 是parser出來了,不過parser回來的字是亂碼
試著加入
$v = mb_convert_encoding($v,"BIG5","UTF-8");
結果會出錯
請教這如何解呢 ?
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 49.158.112.110
※ 文章網址: http://webptt.com/m.aspx?n=bbs/PHP/M.1411883164.A.113.html
1F:→ bibo9901: parse (v) 和 parser (n) 真的那麼難分嗎@@ 09/28 14:02
2F:→ woominin: 不懂什麼意思 - - 09/28 14:14
3F:推 bency: 是一個動詞(parse)跟名詞(parser)的概念 09/28 15:25
4F:→ KawasumiMai: 就好像你去teach一個人跟你去teacher一個人 09/28 16:07
5F:→ KawasumiMai: 後面不會覺得怪怪的嗎 09/28 16:07
6F:推 hit1205: $v 在這裡是一個 DOMElement,但你要轉換編碼的是裡頭的 09/28 22:08
7F:→ hit1205: 字,比如說你要轉 $v->getAttribute('title'); 的話 09/28 22:09
8F:→ hit1205: 那應該是直接轉 $v->getAttribute('title');,而不是 09/28 22:09
9F:→ hit1205: 轉 $v 本身,因為 mb_convert_encoding 是轉字串用的 09/28 22:09
10F:→ hit1205: 如果是要取內文請用 $v->textContent (PHP文件似乎沒提XD 09/28 22:16
11F:→ hit1205: 另外,你讀的這網站本來就是 UTF-8 了,所以其實不用轉碼 09/28 22:16