久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

      <bdo id='wlAOK'></bdo><ul id='wlAOK'></ul>
    <tfoot id='wlAOK'></tfoot><legend id='wlAOK'><style id='wlAOK'><dir id='wlAOK'><q id='wlAOK'></q></dir></style></legend>

    1. <small id='wlAOK'></small><noframes id='wlAOK'>

      1. <i id='wlAOK'><tr id='wlAOK'><dt id='wlAOK'><q id='wlAOK'><span id='wlAOK'><b id='wlAOK'><form id='wlAOK'><ins id='wlAOK'></ins><ul id='wlAOK'></ul><sub id='wlAOK'></sub></form><legend id='wlAOK'></legend><bdo id='wlAOK'><pre id='wlAOK'><center id='wlAOK'></center></pre></bdo></b><th id='wlAOK'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='wlAOK'><tfoot id='wlAOK'></tfoot><dl id='wlAOK'><fieldset id='wlAOK'></fieldset></dl></div>

        PHP DOMDocument 無法處理 utf-8 字符 (☆)

        PHP DOMDocument failing to handle utf-8 characters (☆)(PHP DOMDocument 無法處理 utf-8 字符 (☆))
          • <bdo id='1Cw0F'></bdo><ul id='1Cw0F'></ul>
            <tfoot id='1Cw0F'></tfoot>

                <i id='1Cw0F'><tr id='1Cw0F'><dt id='1Cw0F'><q id='1Cw0F'><span id='1Cw0F'><b id='1Cw0F'><form id='1Cw0F'><ins id='1Cw0F'></ins><ul id='1Cw0F'></ul><sub id='1Cw0F'></sub></form><legend id='1Cw0F'></legend><bdo id='1Cw0F'><pre id='1Cw0F'><center id='1Cw0F'></center></pre></bdo></b><th id='1Cw0F'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='1Cw0F'><tfoot id='1Cw0F'></tfoot><dl id='1Cw0F'><fieldset id='1Cw0F'></fieldset></dl></div>

                  <legend id='1Cw0F'><style id='1Cw0F'><dir id='1Cw0F'><q id='1Cw0F'></q></dir></style></legend>

                  <small id='1Cw0F'></small><noframes id='1Cw0F'>

                    <tbody id='1Cw0F'></tbody>
                  本文介紹了PHP DOMDocument 無法處理 utf-8 字符 (☆)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  網(wǎng)絡(luò)服務(wù)器以 utf-8 編碼提供響應(yīng),所有文件都以 utf-8 編碼保存,我知道的所有設(shè)置都已設(shè)置為 utf-8 編碼.

                  The webserver is serving responses with utf-8 encoding, all files are saved with utf-8 encoding, and everything I know of setting has been set to utf-8 encoding.

                  這是一個快速程序,用于測試輸出是否有效:

                  Here's a quick program, to test if the output works:

                  <?php
                  $html = <<<HTML
                  <!doctype html>
                  <html>
                  <head>
                      <meta charset="utf-8">
                      <title>Test!</title>
                  </head>
                  <body>
                      <h1>☆ Hello ☆ World ☆</h1>
                  </body>
                  </html>
                  HTML;
                  
                  $dom = new DOMDocument("1.0", "utf-8");
                  $dom->loadHTML($html);
                  
                  header("Content-Type: text/html; charset=utf-8");
                  echo($dom->saveHTML());
                  

                  程序的輸出為:

                  <!DOCTYPE html>
                  <html><head><meta charset="utf-8"><title>Test!</title></head><body>
                      <h1>&acirc;&#152;&#134; Hello &acirc;&#152;&#134; World &acirc;&#152;&#134;</h1>
                  </body></html>
                  

                  呈現(xiàn)為:

                  我可能做錯了什么?要告訴 DOMDocument 正確處理 utf-8,我需要具體多少?

                  What could I be doing wrong? How much more specific do I have to be to tell the DOMDocument to handle utf-8 properly?

                  推薦答案

                  DOMDocument::loadHTML() 需要一個 HTML 字符串.

                  DOMDocument::loadHTML() expects a HTML string.

                  HTML 使用 ISO-8859-1 編碼(ISO 拉丁字母第 1 號)作為其規(guī)范的默認(rèn)編碼.這是因為更長的時間,請參閱 6.1.HTML 文檔字符集.實際上,這更多是常見網(wǎng)絡(luò)瀏覽器對 Windows-1252 的默認(rèn)支持.

                  HTML uses the ISO-8859-1 encoding (ISO Latin Alphabet No. 1) as default per it's specs. That is since longer, see 6.1. The HTML Document Character Set. In reality that is more the default support for Windows-1252 in common webbrowsers.

                  我回過頭來是因為 PHP 的 DOMDocument 基于 libxml 并且?guī)砹?HTMLparser 專為 HTML 4.0 設(shè)計.

                  I go back that far because PHP's DOMDocument is based on libxml and that brings the HTMLparser which is designed for HTML 4.0.

                  我認(rèn)為可以安全地假設(shè)您可以加載 ISO-8859-1 編碼的字符串.

                  I'd say it's safe to assume then that you can load an ISO-8859-1 encoded string.

                  您的字符串是 UTF-8 編碼的.將所有高于 127/h7F 的字符轉(zhuǎn)換為 HTML 實體 就可以了.如果您不想自己做,那就是 mb_convert_encodingHTML-ENTITIES 目標(biāo)編碼所做的:

                  Your string is UTF-8 encoded. Turn all characters higher than 127 / h7F into HTML Entities and you're fine. If you don't want to do that your own, that is what mb_convert_encoding with the HTML-ENTITIES target encoding does:

                  • 那些具有命名實體的字符,將獲得命名實體.<代碼>€ ->&歐元;
                  • 其他人獲得他們的數(shù)字(十進(jìn)制)實體,例如<代碼>☆ ->&#9734;

                  以下代碼示例通過使用回調(diào)函數(shù)使進(jìn)度更加明顯:

                  The following is a code example that makes the progress a bit more visible by using a callback function:

                  $html = preg_replace_callback('/[x{80}-x{10FFFF}]/u', function($match) {
                      list($utf8) = $match;
                      $entity = mb_convert_encoding($utf8, 'HTML-ENTITIES', 'UTF-8');
                      printf("%s -> %s
                  ", $utf8, $entity);
                      return $entity;
                  }, $html);
                  

                  您的字符串的示例輸出:

                  This exemplary outputs for your string:

                  ☆ -> &#9734;
                  ☆ -> &#9734;
                  ☆ -> &#9734;
                  

                  無論如何,這只是為了更深入地了解您的字符串.您希望將其轉(zhuǎn)換為 loadHTML 可以處理的編碼.這可以通過將 US-ASCII 之外的所有內(nèi)容轉(zhuǎn)換為 HTML 實體來實現(xiàn):

                  Anyway, that's just for looking deeper into your string. You want to have it either converted into an encoding loadHTML can deal with. That can be done by converting all outside of US-ASCII into HTML Entities:

                  $us_ascii = mb_convert_encoding($utf_8, 'HTML-ENTITIES', 'UTF-8');
                  

                  請注意您的輸入實際上是 UTF-8 編碼的.如果您甚至有混合編碼(某些輸入可能會發(fā)生這種情況)mb_convert_encoding 只能處理每個字符串一種編碼.我已經(jīng)在上面概述了如何在正則表達(dá)式的幫助下更具體地進(jìn)行字符串替換,所以我現(xiàn)在留下更多細(xì)節(jié).

                  Take care that your input is actually UTF-8 encoded. If you have even mixed encodings (that can happen with some inputs) mb_convert_encoding can only handle one encoding per string. I already outlined above how to more specifically do string replacements with the help of regular expressions, so I leave further details for now.

                  另一種選擇是提示編碼.這可以通過修改文檔并添加

                  The other alternative is to hint the encoding. This can be done in your case by modifying the document and adding a

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

                  這是一個指定字符集的內(nèi)容類型.對于無法通過網(wǎng)絡(luò)服務(wù)器使用的 HTML 字符串(例如,保存在磁盤上或在您的示例中的字符串中),這也是最佳實踐.網(wǎng)絡(luò)服務(wù)器通常將其設(shè)置為響應(yīng)標(biāo)頭.

                  which is a Content-Type specifying a charset. That is also best practice for HTML strings that are not available via a webserver (e.g. saved on disk or inside a string like in your example). The webserver normally set's that as the response header.

                  如果你不關(guān)心錯位的警告,你可以把它加在字符串前面:

                  If you don't care the misplaced warnings, you can just add it in front of the string:

                  $dom = new DomDocument();
                  $dom->loadHTML('<meta http-equiv="content-type" content="text/html; charset=utf-8">'.$html);
                  

                  根據(jù) HTML 2.0 規(guī)范,只能出現(xiàn)在文檔的 部分的元素將自動放置在那里.這也是這里發(fā)生的事情.輸出(漂亮的打印):

                  Per the HTML 2.0 specs, elements that can only appear in the <head> section of a document, will be automatically placed there. This is what happens here, too. The output (pretty-print):

                  <!DOCTYPE html>
                  <html>
                    <head>
                      <meta http-equiv="content-type" content="text/html; charset=utf-8">
                      <meta charset="utf-8">
                      <title>Test!</title>
                    </head>
                    <body>
                      <h1>☆ Hello ☆ World ☆</h1>    
                    </body>
                  </html>
                  

                  這篇關(guān)于PHP DOMDocument 無法處理 utf-8 字符 (☆)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死鎖異常代碼?)
                  PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滾動游標(biāo)不起作用)
                  PHP PDO ODBC connection(PHP PDO ODBC 連接)
                  Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔術(shù)方法)
                  php pdo get only one value from mysql; value that equals to variable(php pdo 只從 mysql 獲取一個值;等于變量的值)
                  MSSQL PDO could not find driver(MSSQL PDO 找不到驅(qū)動程序)
                    <i id='fklgy'><tr id='fklgy'><dt id='fklgy'><q id='fklgy'><span id='fklgy'><b id='fklgy'><form id='fklgy'><ins id='fklgy'></ins><ul id='fklgy'></ul><sub id='fklgy'></sub></form><legend id='fklgy'></legend><bdo id='fklgy'><pre id='fklgy'><center id='fklgy'></center></pre></bdo></b><th id='fklgy'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='fklgy'><tfoot id='fklgy'></tfoot><dl id='fklgy'><fieldset id='fklgy'></fieldset></dl></div>

                    1. <legend id='fklgy'><style id='fklgy'><dir id='fklgy'><q id='fklgy'></q></dir></style></legend>

                          <tbody id='fklgy'></tbody>
                      1. <tfoot id='fklgy'></tfoot>

                        • <small id='fklgy'></small><noframes id='fklgy'>

                            <bdo id='fklgy'></bdo><ul id='fklgy'></ul>
                            主站蜘蛛池模板: 国产精品一区二区福利视频 | 久久久蜜桃 | 精品一区二区三区入口 | 久一久| 午夜国产一级片 | 午夜免费福利片 | 国产精品一区二区三 | 久久久成人免费一区二区 | 青青草网 | 人人干人人看 | 国产一区二区三区久久久久久久久 | 国产在线高清 | 亚洲天堂久久 | 亚洲一区二区在线播放 | 国产激情精品一区二区三区 | 九九爱这里只有精品 | a级在线免费| 欧美激情va永久在线播放 | 亚洲一区二区三区免费在线观看 | 国产欧美一区二区三区日本久久久 | 麻豆av在线免费观看 | 亚洲成人免费视频在线 | 亚洲高清一区二区三区 | 国产精品欧美一区二区三区不卡 | 中文字幕在线观看av | 久久九精品 | 超碰在线人人干 | 欧美一级网站 | 一区二区三区视频 | 亚洲一区二区三区久久 | 亚洲成色777777在线观看影院 | 国产电影一区二区 | 欧美日本一区二区 | 激情欧美日韩一区二区 | 日韩av一区二区在线观看 | 鲁视频 | 日本精品免费在线观看 | 成人国产精品久久 | 免费艹逼视频 | 欧美一级黑人aaaaaaa做受 | 国产乱码久久久久久 |