[請益] 編碼的問題…

看板PHP作者 (沒事就好)時間11年前 (2014/10/01 16:41), 編輯推噓5(506)
留言11則, 5人參與, 最新討論串1/1
不好意思,如標題說的,小弟遇到一個轉碼的問題 想請教一下 以下為程式碼 <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

10/01 17:00, , 1F
可以用 mb_convert_encoding 把 BIG5 轉成 UTF-8
10/01 17:00, 1F

10/01 17:11, , 2F
$ment = mb_convert_encoding($ment,"BIG5","UTF-8");
10/01 17:11, 2F

10/01 17:11, , 3F
這樣加在 foreach 跟 echo 之間 會有錯 請問是哪裡錯
10/01 17:11, 3F

10/01 19:23, , 4F
你的檔案有存為utf8不含bom格式嗎
10/01 19:23, 4F

10/01 19:40, , 5F
應該不是樓上的問題; 原PO加 convert 後 foreach 有 {} 嗎?
10/01 19:40, 5F

10/01 22:40, , 6F
有,不過是一樣的,要嘛就上面亂碼,要嘛就下面亂碼 - -
10/01 22:40, 6F

10/02 08:35, , 7F
突然發現一件事: $ment 應該是個 DOM 物件而不單純是字串
10/02 08:35, 7F

10/02 08:36, , 8F
你試試對 $ment->innertext 轉換, 這應該就是實際字串了
10/02 08:36, 8F

10/02 08:36, , 9F
也就是變成 echo mb_convert_encoding($ment->innertext,..
10/02 08:36, 9F

10/02 16:44, , 10F
樓上L大,解決了,如同你說的 感謝呀
10/02 16:44, 10F

10/02 21:13, , 11F
提醒一下原po,你的 #1K9w2S4J 是一樣的原因喔 XD
10/02 21:13, 11F
文章代碼(AID): #1KAxuU6N (PHP)
文章代碼(AID): #1KAxuU6N (PHP)