gpt4 book ai didi

php - 显示“”字符而不是 “ ”

转载 作者:行者123 更新时间:2023-12-02 06:16:40 24 4
gpt4 key购买 nike

我发现this thread很好地描述了我的问题,而this答案恰好描述了我的问题。

The non-breaking space character is byte 0xA0 is ISO-8859-1; when encoded to UTF-8 it'd be 0xC2,0xA0, which, if you (incorrectly) view it as ISO-8859-1 comes out as "Â ". That includes a trailing nbsp...



但是,我设法将问题归结为用于将图像标签包装在div中的函数。
function img_format($str)
{
$doc = new DOMDocument();
@$doc->loadHTML($str); // <-- Bonus points for the explaination of the @

// $tags object
$tags = $doc->getElementsByTagName('img');

foreach ($tags as $tag) {

$div = $doc->createElement('div');
$div->setAttribute('class','inner-copy');
$tag->parentNode->insertBefore($div, $tag);
$div->appendChild($tag);

$tag->setAttribute('class', 'inner-img');
}

$str = $doc->saveHTML();

return $str;
}

很简单,如何在此函数中解决此问题?

我了解使用;
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />

可以解决此问题,但是在函数本身中显然存在一些我忽略的问题。

我试过了;
$dom->validateOnParse = true;

无济于事。 (我也不知道该怎么做)

最佳答案

找到了!

@$doc->loadHTML(mb_convert_encoding($str, 'HTML-ENTITIES', 'UTF-8'));

This answer解释了这个问题并给出了上面的解决方法;

DOMDocument::loadHTML will treat your string as being in ISO-8859-1 unless you tell it otherwise. This results in UTF-8 strings being interpreted incorrectly.

关于php - 显示“”字符而不是 “&nbsp;”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28502051/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com