前段时间我问how to properly convert HTML encodings,我得到了答案。从那以后我就用这个答案了,但是今天我注意到mb_convert_encoding
的奇怪行为。根据您运行的位置,输出方式不同:
Browser (Tested in Chrome and Firefox):
W/o mb: 42Â sp
mb: 42 sp
PHP CLI:
W/o mb: 31 sp
mb: 31�sp
使用浏览器,mb_convert_encoding
输出正确,但另一方面,使用命令行,没有mb_convert_encoding
是正确的输出。我错过了什么吗?提前谢谢!
$str_html = $this->curlHelper->file_get_contents_curl($page);
$dom = new DOMDocument();
$dom->resolveExternals = true;
$dom->substituteEntities = false;
@$dom->loadHTML($str_html);
$xpath = new DomXpath($dom);
(...)
foreach ($table_lines as $line) {
$tds = $line->childNodes;
$sp = mb_convert_encoding($tds->item(0)->nodeValue,
'ISO-8859-1', 'UTF-8');
echo "W/o mb: " . $tds->item(0)->nodeValue . PHP_EOL;
echo "mb: " . $sp . PHP_EOL;
}