DomDocument和特殊字符

Fra*_*ula 21 php utf-8 domdocument

这是我的代码:

$oDom = new DOMDocument();
$oDom->loadHTML("èàéìòù");
echo $oDom->saveHTML();
Run Code Online (Sandbox Code Playgroud)

这是输出:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p>&Atilde;&uml;&Atilde;&nbsp;&Atilde;&copy;&Atilde;&not;&Atilde;&sup2;&Atilde;&sup1;</p></body></html>
Run Code Online (Sandbox Code Playgroud)

我想要这个输出:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p>èàéìòù</p></body></html>
Run Code Online (Sandbox Code Playgroud)

我试过......

$oDom = new DomDocument('4.0', 'UTF-8');
Run Code Online (Sandbox Code Playgroud)

或1.0和其他东西,但没有.

另一件事......有一种方法可以获得相同的未触动的HTML?例如,输入中的html 使用DOMDocument <p>hello!</p>获取相同的输出,<p>hello!</p>仅用于解析DOM并在标记内进行一些替换.

Fra*_*ula 42

解:

$oDom = new DOMDocument();
$oDom->encoding = 'utf-8';
$oDom->loadHTML( utf8_decode( $sString ) ); // important!

$sHtml = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
$sHtml .= $oDom->saveHTML( $oDom->documentElement ); // important!
Run Code Online (Sandbox Code Playgroud)

saveHTML()方法以不同方式指定节点.您可以使用主节点($oDom->documentElement)!DOCTYPE手动添加所需的.另一件重要的事情是utf8_decode().DOMDocument在我的例子中,类的所有属性和其他方法都不会产生所需的结果.

  • 要使其与ISO-8859-1集之外的其他字符一起使用,您需要使用多字节解码.因此,中文或欧元符号等字符也可以正确编码.`$ oDom-> loadHTML(mb_convert_encoding($ sString,'HTML-ENTITIES','UTF-8'));`[详见更多信息](http://stackoverflow.com/questions/8218230/php-domdocument -loadhtml-不编码UTF-8-正确地) (7认同)

小智 6

加载HTML 后,尝试设置编码类型.

$dom = new DOMDocument();
$dom->loadHTML($data);
$dom->encoding = 'utf-8';
echo $dom->saveHTML();
Run Code Online (Sandbox Code Playgroud)

另一种方式


Nur*_*iko 6

我不知道为什么标记的答案对我的问题不起作用。但这个做到了。

参考:https: //www.php.net/manual/en/class.domdocument.php

<?php

            // checks if the content we're receiving isn't empty, to avoid the warning
            if ( empty( $content ) ) {
                return false;
            }

            // converts all special characters to utf-8
            $content = mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8');

            // creating new document
            $doc = new DOMDocument('1.0', 'utf-8');

            //turning off some errors
            libxml_use_internal_errors(true);

            // it loads the content without adding enclosing html/body tags and also the doctype declaration
            $doc->LoadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

            // do whatever you want to do with this code now

?>
Run Code Online (Sandbox Code Playgroud)


小智 6

$dom = new DomDocument();
$str = htmlentities($str);
$dom->loadHTML(utf8_decode($str));
$dom->encoding = 'utf-8';
.
.
.
$str = $dom->saveHTML();
$str = html_entity_decode($str);
Run Code Online (Sandbox Code Playgroud)

上面的代码对我有用。


小智 5

根据php.net手册页上的用户评论,这个问题似乎是众所周知的.解决方案建议包括推杆

<meta http-equiv="content-type" content="text/html; charset=utf-8">
Run Code Online (Sandbox Code Playgroud)

在将任何带有非ASCII字符的字符串放入文档之前的文档中.

另一个黑客建议推杆

<?xml encoding="UTF-8">
Run Code Online (Sandbox Code Playgroud)

作为文档中的第一个文本,然后在最后删除它.

讨厌的东西.闻起来像个臭虫给我.