看板 PHP 關於我們 聯絡資訊
不好意思,如標題說的,小弟遇到一個轉碼的問題 想請教一下 以下為程式碼 <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <?php include('simple_html_dom.php'); $dom = file_get_html('http://na3tvilla.ttbnb.net/about.htm'); foreach($dom->find('table.abfont') as $element) echo $element->innertext; $dom = file_get_html('http://na3tvilla.ttbnb.net/about.htm'); foreach($dom->find('title') as $ment) echo $ment->innertext; ?> 這樣執行出來後 echo $ment->innertext; 所出來的會是亂碼 但要是我把最上面 head裡的編碼拿掉 就會變中文 但是 echo $element->innertext; 所出來的就會變亂碼… 這該怎麼解決呢 ?? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.51.223.4 ※ 文章網址: http://www.ptt.cc/bbs/PHP/M.1412152862.A.197.html
allanshen: 可以用 mb_convert_encoding 把 BIG5 轉成 UTF-8 10/01 17:00
woominin: $ment = mb_convert_encoding($ment,"BIG5","UTF-8"); 10/01 17:11
woominin: 這樣加在 foreach 跟 echo 之間 會有錯 請問是哪裡錯 10/01 17:11
loktarrr: 你的檔案有存為utf8不含bom格式嗎 10/01 19:23
LPH66: 應該不是樓上的問題; 原PO加 convert 後 foreach 有 {} 嗎? 10/01 19:40
woominin: 有,不過是一樣的,要嘛就上面亂碼,要嘛就下面亂碼 - - 10/01 22:40
LPH66: 突然發現一件事: $ment 應該是個 DOM 物件而不單純是字串 10/02 08:35
LPH66: 你試試對 $ment->innertext 轉換, 這應該就是實際字串了 10/02 08:36
LPH66: 也就是變成 echo mb_convert_encoding($ment->innertext,.. 10/02 08:36
woominin: 樓上L大,解決了,如同你說的 感謝呀 10/02 16:44
hit1205: 提醒一下原po,你的 #1K9w2S4J 是一樣的原因喔 XD 10/02 21:13