DOMDocument 中的 nodeValue 在 PHP 中返回奇怪的字符

发布于 2024-08-17 07:32:31 字数 291 浏览 3 评论 0原文

所以我试图解析 HTML 页面并使用 get_elements_by_tag_name('p'); 查找段落 (

)

问题是当我使用 $element->nodeValue,它返回奇怪的字符。首先使用curl 将文档加载到$html 中,然后将其加载到DOMDocument 中。

我确信这与字符集有关。

以下是响应的示例:“aujourdàhui”。

提前致谢。

So I'm trying to parse HTML pages and looking for paragraphs (<p>) using get_elements_by_tag_name('p');

The problem is that when I use $element->nodeValue, it's returning weird characters. The document is loaded first into $html using curl then loading it into a DOMDocument.

I'm sure it has to do with charsets.

Here's an example of a response: "aujourd’hui".

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

薄凉少年不暖心 2024-08-24 07:32:31

我遇到了同样的问题,现在注意到 loadHTML() 不再需要 2 个参数,所以我必须找到不同的解决方案。使用 DOM 库中的以下函数,我能够从 HTML 内容中删除时髦的字符。

private static function load_html($html)
{
    $doc = new DOMDocument;
    $doc->loadHTML('<?xml encoding="UTF-8">' . $html);

    foreach ($doc->childNodes as $node)
        if ($node->nodeType == XML_PI_NODE)
            $doc->removeChild($node);

    $doc->encoding = 'UTF-8';

    return $doc;
}

I had the same issues and now noticed that loadHTML() no longer takes 2 parameters, so I had to find a different solution. Using the following function in my DOM library, I was able to remove the funky characters from my HTML content.

private static function load_html($html)
{
    $doc = new DOMDocument;
    $doc->loadHTML('<?xml encoding="UTF-8">' . $html);

    foreach ($doc->childNodes as $node)
        if ($node->nodeType == XML_PI_NODE)
            $doc->removeChild($node);

    $doc->encoding = 'UTF-8';

    return $doc;
}
万劫不复 2024-08-24 07:32:31

显然,上述方法对我来说都不起作用,最后我找到了以下内容:

// Create a DOMDocument instance 
$doc = new DOMDocument();

// The fix: mb_convert_encoding conversion
$doc->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'));

来源和更多信息

Apparently for me none of the above worked, finally I've found the following:

// Create a DOMDocument instance 
$doc = new DOMDocument();

// The fix: mb_convert_encoding conversion
$doc->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'));

Source and more info

且行且努力 2024-08-24 07:32:31

我通过强制转换为 UTF-8 解决了这个问题,即使原始文本是 UTF-8:

$text = iconv("UTF-8", "UTF-8", $text);
$dom = new SmartDOMDocument();
$dom->loadHTML($webpage, 'UTF-8');
.
.
echo $node->nodeValue;

PHP 很奇怪:)

I fixed this by forcing conversion to UTF-8 even though the original text was UTF-8:

$text = iconv("UTF-8", "UTF-8", $text);
$dom = new SmartDOMDocument();
$dom->loadHTML($webpage, 'UTF-8');
.
.
echo $node->nodeValue;

PHP is wierd :)

╰沐子 2024-08-24 07:32:31

这是一个编码问题。尝试将编码显式设置为 UTF-8。

这应该有帮助: http://devzone.zend.com/article/8855

This is an encoding issue. try explicitly setting the encoding to UTF-8.

this should help: http://devzone.zend.com/article/8855

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文