我正在使用DOMDocument来提取一些段落。
以下是我正在启动的初始 htm 文件的样子:
<html>
<head>
<title>Toxins</title>
</head>
<body>
<p class=8reference><span>1.</span><span>Sivonen, K.; Jones, G. Cyanobacterial Toxins. In <i>Toxic Cyanobacteria in Water. A Guide to Their Public Health Consequences, Monitoring and Management</i>; Chorus, I., Bartram, J., Eds.; E. and F.N. Spon: London, UK, 1999; pp. 41–111.</span></p>
</body>
</html>
当我在做:
$dom_input = new 'DOMDocument("1.0","UTF-8");
$dom_input->encoding = "UTF-8";
$dom_input->formatOutput = true;
$dom_input->loadHTMLFile($manuscript->getUploadRootDir().$manuscript->getFileName());
$paragraphs = $dom_input->getElementsByTagName('p');
foreach ($paragraphs as $paragraph) {
if($paragraph->getAttribute('class') == "8reference") {
var_dump($paragraph->nodeValue);
}
}
从"第 41–111 页"的破折号转换为
pp. 41–111
知道为什么以及如何修复它以获取 utf8 unicode 值吗?
提前谢谢你。
在我看来
,数据是正确的,你只是显示不正确。
您是否使用 UTF-8 输出?
à + 事物是经典的"显示 UTF-8 编码数据,就好像它不是 UTF-8 一样。
例如如果要输出到 Web 浏览器,请尝试使用元标记设置字符集。 例如
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
如果您需要以 UTF-8 以外的内容输出,则需要先转换为替代字符集。
使用 PHP fputcsv()
生成 CSV 文件时。在将数据插入到fputcsv()
之前使用此选项
$data = mb_convert_encoding($data, 'cp1252', 'utf-8');
fputcsv($file, $data);
这肯定会在生成 CSV 时停止将破折号转换为â€"
。