【问题标题】:PHP DOMDocument - why is en dash "–" converted to –PHP DOMDocument - 为什么将破折号“–”转换为–
【发布时间】:2013-11-26 09:49:17
【问题描述】:

我正在使用 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);
    }
}

“pp. 41–111”中的破折号被转换为

pp. 41–111

知道为什么以及如何修复它以获得 utf8 unicode 值吗?

提前谢谢你。

【问题讨论】:

  • 可能是因为它是 m-dash 或 n-dash 而不是简单的 ASCII 连字符
  • 您是转储到控制台还是浏览器?无论哪种情况,输出通道是否配置为显示 utf8?控制台中的 LANG= 还是浏览器中的 Content-TYPE?

标签: php xml utf-8 domdocument


【解决方案1】:

在我看来数据是正确的,你只是显示不正确。

您是否以 UTF-8 输出?

Ã+ 是一个经典的“显示 UTF-8 编码数据,就好像它不是 UTF-8 一样。

例如 如果您要输出到 Web 浏览器,请尝试使用元标记设置字符集。例如

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

如果您需要以 UTF-8 以外的格式输出,则需要先转换为替代字符集。

【讨论】:

  • 谢谢,问题解决了。
【解决方案2】:

使用 PHP fputcsv() 生成 CSV 文件时。在向fputcsv()插入数据之前使用它

$data = mb_convert_encoding($data, 'cp1252', 'utf-8');
fputcsv($file, $data);

这肯定会在生成 CSV 时停止将破折号转换为â€"

【讨论】:

    猜你喜欢
    • 2013-04-29
    • 1970-01-01
    • 2015-05-12
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-03
    相关资源
    最近更新 更多