[英]How to change encoding from plain text to Unicode so that I can read special characters from a HTML?
下面是我的代码:
<?php
// example of how to use basic selector to retrieve HTML contents
include('/Library/WebServer/Documents/simple_html_dom.php'); //this is the api for the simplehtmldom
// get DOM from URL or file
$html = file_get_html('http:/www.google.hk');
// extract text from table
echo $html->find('td[align="top"]', 1)->innertext.'<br><hr>';
// extract text from HTML
echo $html->innertext;
?>
我正在使用simplephphtmldon
API。 当我在本地服务器上执行php程序时,由于纯文本无法真正编码它们以使其像预期的那样显示,因此我得到了很多无法识别的字符。 有人可以告诉我我需要更改为inner text
才能显示所有字符吗? PS我也没有任何运气尝试plaintext
。 textContent
在我看来似乎很textContent
。 也许我需要先尝试其他元素(?)。 谢谢
echo utf8_encode($html->innertext);
要么
echo utf8_decode($html->innertext);
它取决于原始编码,因此您可能要同时尝试两者。
注意:如果在浏览器上看到输出,请确保将Unicode
设置为文本编码,或在脚本顶部使用以下代码。
header('Content-Type: text/html; charset=utf-8');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.