[英]DomDocument and special characters
这是我的代码:
$oDom = new DOMDocument();
$oDom->loadHTML("èàéìòù");
echo $oDom->saveHTML();
这是 output:
<!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>
我想要这个 output:
<!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>
我试过...
$oDom = new DomDocument('4.0', 'UTF-8');
或 1.0 和其他东西,但什么都没有。
另一件事......有没有办法获得相同的原样HTML? 例如,在输入<p>hello!</p>
中使用此 html 获得相同的 output <p>hello!</p>
使用 DOMDocument 仅用于解析 DOM 并在标签内进行一些替换。
解决方案:
$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!
saveHTML()
方法在指定节点时的工作方式不同。 您可以使用主节点 ( $oDom->documentElement
) 手动添加所需的!DOCTYPE
。 另一个重要的事情是utf8_decode()
。 就我而言, DOMDocument
class 的所有属性和其他方法都不会产生预期的结果。
加载 HTML后尝试设置编码类型。
$dom = new DOMDocument();
$dom->loadHTML($data);
$dom->encoding = 'utf-8';
echo $dom->saveHTML();
$dom = new DomDocument();
$str = htmlentities($str);
$dom->loadHTML(utf8_decode($str));
$dom->encoding = 'utf-8';
.
.
.
$str = $dom->saveHTML();
$str = html_entity_decode($str);
上面的代码对我有用。
根据php.net 手册页上的用户评论,这个问题似乎是已知的。 那里建议的解决方案包括
<meta http-equiv="content-type" content="text/html; charset=utf-8">
在将任何带有非 ASCII 字符的字符串放入文档之前。
另一个黑客建议把
<?xml encoding="UTF-8">
作为文档中的第一个文本,然后在最后将其删除。
讨厌的东西。 对我来说闻起来像个虫子。
这边走:
/**
* @param string $text
* @return DOMDocument
*/
private function buildDocument($text)
{
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">' . $text);
libxml_use_internal_errors(false);
return $dom;
}
我不知道为什么标记的答案对我的问题不起作用。 但是这个做到了。
参考: 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
?>
对我有用的是:
$doc->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'));
看起来您只需要在创建 DOMDocument object 时设置替代实体。
以上都没有为我工作,但这个工作完成了:
$fileContent = file_get_contents('my_file.html');
$dom = new DOMDocument();
@$dom->loadHTML(mb_convert_encoding($fileContent, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$dom->encoding = 'utf-8';
$html = $dom->saveHTML();
$html = html_entity_decode($html, ENT_COMPAT, 'UTF-8');
echo $html;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.