【发布时间】:2015-04-14 15:21:08
【问题描述】:
我发现this thread 很好地描述了我的问题,this 的答案准确地描述了我的问题。
不间断空格字符是字节0xA0是ISO-8859-1;当编码为 UTF-8 时,它将是 0xC2,0xA0,如果您(错误地)将其视为 ISO-8859-1,则会显示为
"Â "。这包括一个尾随的 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($str);的方法调用抛出错误,您将不会看到错误/警告消息。您可以在PHP documentation 中了解更多信息。
标签: php dom domdocument