【问题标题】:DomDocument and special charactersDomDocument 和特殊字符
【发布时间】:2011-09-28 05:33:44
【问题描述】:

这是我的代码:

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

这是输出:

<!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>

我想要这个输出:

<!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? 例如,在输入 &lt;p&gt;hello!&lt;/p&gt; 中使用此 html 获得相同的输出 &lt;p&gt;hello!&lt;/p&gt; 使用 DOMDocument 仅用于解析 DOM 并在标签内进行一些替换。

【问题讨论】:

标签: php utf-8 domdocument


【解决方案1】:

解决方案:

$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-&gt;documentElement) 手动添加所需的 !DOCTYPE。 另一个重要的事情是utf8_decode()。 在我的例子中,DOMDocument 类的所有属性和其他方法都不会产生预期的结果。

【讨论】:

  • 我在加载页面的一部分时正在寻找一种编码解决方案。上面的解决方案完美!感谢分享。
  • 要使其与 ISO-8859-1 集之外的其他字符一起使用,您需要使用多字节解码。这样像中文或欧元符号这样的字符也可以正确编码。 $oDom-&gt;loadHTML(mb_convert_encoding($sString, 'HTML-ENTITIES', 'UTF-8'));see here for more info
  • 试图解决这个问题我差点失去理智!非常感谢!
【解决方案2】:

在您加载 HTML
之后尝试设置编码类型。

$dom = new DOMDocument();
$dom->loadHTML($data);
$dom->encoding = 'utf-8';
echo $dom->saveHTML();

Other way

【讨论】:

    【解决方案3】:
    $dom = new DomDocument();
    $str = htmlentities($str);
    $dom->loadHTML(utf8_decode($str));
    $dom->encoding = 'utf-8';
    .
    .
    .
    $str = $dom->saveHTML();
    $str = html_entity_decode($str);
    

    上面的代码对我有用。

    【讨论】:

      【解决方案4】:

      manual page at php.net 上的用户 cmets 表示,该问题似乎是已知的。建议的解决方案包括放置

      <meta http-equiv="content-type" content="text/html; charset=utf-8">
      

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

      另一个 hack 建议把

      <?xml encoding="UTF-8">
      

      作为文档中的第一个文本,然后在末尾删除它。

      讨厌的东西。对我来说闻起来像个虫子。

      【讨论】:

        【解决方案5】:

        这边:

        /**
         * @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;
        }
        

        【讨论】:

        • 我需要它作为移动应用程序使用的 API 端点。只有这个解决方案对我有用。谢谢:)
        【解决方案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
        
        ?>
        

        【讨论】:

          【解决方案7】:

          以上都没有对我有用,但这个成功了:

          $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;
          

          【讨论】:

          • 此解决方案适用于我的情况。非常感谢。
          【解决方案8】:

          看起来您只需要在创建 DOMDocument 对象时设置substituteEntities。

          【讨论】:

            【解决方案9】:

            对我有用的是:

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

            信用:https://davidwalsh.name/domdocument-utf8-problem

            【讨论】:

              猜你喜欢
              • 2019-07-08
              • 2013-08-25
              • 1970-01-01
              • 2011-04-23
              • 2018-08-05
              • 1970-01-01
              • 2013-10-14
              • 2015-07-10
              • 2012-09-24
              相关资源
              最近更新 更多