pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

      <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="3dhbprt" id='wlAOK'><tfoot id='wlAOK'></tfoot><dl id='wlAOK'><fieldset id='wlAOK'></fieldset></dl></div>

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

        PHP DOMDocument failing to handle utf-8 characters (☆)(PHP DOMDocument 無(wú)法處理 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="hflt7hl" 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 無(wú)法處理 utf-8 字符 (☆)的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                  問(wèn)題描述

                  網(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.

                  這是一個(gè)快速程序,用于測(cè)試輸出是否有效:

                  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)為:

                  我可能做錯(cuò)了什么?要告訴 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() 需要一個(gè) HTML 字符串.

                  DOMDocument::loadHTML() expects a HTML string.

                  HTML 使用 ISO-8859-1 編碼(ISO 拉丁字母第 1 號(hào))作為其規(guī)范的默認(rèn)編碼.這是因?yàn)楦L(zhǎng)的時(shí)間,請(qǐng)參閱 6.1.HTML 文檔字符集.實(shí)際上,這更多是常見(jiàn)網(wǎng)絡(luò)瀏覽器對(duì) 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.

                  我回過(guò)頭來(lái)是因?yàn)?PHP 的 DOMDocument 基于 libxml 并且?guī)?lái)了 HTMLparser 專(zhuān)為 HTML 4.0 設(shè)計(jì).

                  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 實(shí)體 就可以了.如果您不想自己做,那就是 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í)體的字符,將獲得命名實(shí)體.<代碼>€ ->&歐元;
                  • 其他人獲得他們的數(shù)字(十進(jìn)制)實(shí)體,例如<代碼>☆ ->&#9734;

                  以下代碼示例通過(guò)使用回調(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;
                  

                  無(wú)論如何,這只是為了更深入地了解您的字符串.您希望將其轉(zhuǎn)換為 loadHTML 可以處理的編碼.這可以通過(guò)將 US-ASCII 之外的所有內(nèi)容轉(zhuǎn)換為 HTML 實(shí)體來(lái)實(shí)現(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');
                  

                  請(qǐng)注意您的輸入實(shí)際上是 UTF-8 編碼的.如果您甚至有混合編碼(某些輸入可能會(huì)發(fā)生這種情況)mb_convert_encoding 只能處理每個(gè)字符串一種編碼.我已經(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.

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

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

                  這是一個(gè)指定字符集的內(nèi)容類(lèi)型.對(duì)于無(wú)法通過(guò)網(wǎng)絡(luò)服務(wù)器使用的 HTML 字符串(例如,保存在磁盤(pán)上或在您的示例中的字符串中),這也是最佳實(shí)踐.網(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)心錯(cuò)位的警告,你可以把它加在字符串前面:

                  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)在文檔的 部分的元素將自動(dòng)放置在那里.這也是這里發(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 無(wú)法處理 utf-8 字符 (☆)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(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 可滾動(dòng)游標(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 獲取一個(gè)值;等于變量的值)
                  MSSQL PDO could not find driver(MSSQL PDO 找不到驅(qū)動(dòng)程序)
                    <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="5d57dx7" 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>
                            主站蜘蛛池模板: 淘趣英语网 - 在线英语学习,零基础英语学习网站 | 东莞压铸厂_精密压铸_锌合金压铸_铝合金压铸_压铸件加工_东莞祥宇金属制品 | 陕西鹏展科技有限公司 | 硫化罐-胶管硫化罐-山东鑫泰鑫智能装备有限公司 | 玉米深加工设备-玉米深加工机械-新型玉米工机械生产厂家-河南粮院机械制造有限公司 | 高压负荷开关-苏州雷尔沃电器有限公司 | lcd条形屏-液晶长条屏-户外广告屏-条形智能显示屏-深圳市条形智能电子有限公司 | 武汉高温老化房,恒温恒湿试验箱,冷热冲击试验箱-武汉安德信检测设备有限公司 | PC阳光板-PC耐力板-阳光板雨棚-耐力板雨棚,厂家定制[优尼科板材] | 电采暖锅炉_超低温空气源热泵_空气源热水器-鑫鲁禹电锅炉空气能热泵厂家 | 润滑脂-高温润滑脂-轴承润滑脂-食品级润滑油-索科润滑油脂厂家 | 元拓建材集团官方网站 | 中央空调温控器_风机盘管温控器_智能_液晶_三速开关面板-中央空调温控器厂家 | 【铜排折弯机,钢丝折弯成型机,汽车发泡钢丝折弯机,线材折弯机厂家,线材成型机,铁线折弯机】贝朗折弯机厂家_东莞市贝朗自动化设备有限公司 | 西门子代理商_西门子变频器总代理-翰粤百科| 荣事达手推洗地机_洗地机厂家_驾驶式扫地机_工业清洁设备 | 航拍_专业的无人机航拍摄影门户社区网站_航拍网 | 河南不锈钢水箱_地埋水箱_镀锌板水箱_消防水箱厂家-河南联固供水设备有限公司 | 电地暖-电采暖-发热膜-石墨烯电热膜品牌加盟-暖季地暖厂家 | atcc网站,sigma试剂价格,肿瘤细胞现货,人结肠癌细胞株购买-南京科佰生物 | 纯化水设备-纯水设备-超纯水设备-[大鹏水处理]纯水设备一站式服务商-东莞市大鹏水处理科技有限公司 | 中药超微粉碎机(中药细胞级微粉碎)-百科 | 上海刑事律师|刑事辩护律师|专业刑事犯罪辩护律师免费咨询-[尤辰荣]金牌上海刑事律师团队 | 亿诺千企网-企业核心产品贸易 | 【北京写字楼出租_写字楼租赁_办公室出租网/出售】-远行地产官网 | 百方网-百方电气网,电工电气行业专业的B2B电子商务平台 | 衬氟旋塞阀-卡套旋塞阀-中升阀门首页 | 沉降天平_沉降粒度仪_液体比重仪-上海方瑞仪器有限公司 | 全屋整木定制-橱柜,家具定制-四川峨眉山龙马木业有限公司 | 拉力机-拉力试验机-万能试验机-电子拉力机-拉伸试验机-剥离强度试验机-苏州皖仪实验仪器有限公司 | 航空障碍灯_高中低光强航空障碍灯_民航许可认证航空警示灯厂家-东莞市天翔航天科技有限公司 | 英国公司注册-新加坡公司注册-香港公司开户-离岸公司账户-杭州商标注册-杭州优创企业 | 高压贴片电容|贴片安规电容|三端滤波器|风华电容代理南京南山 | 散热器厂家_暖气片_米德尔顿散热器| 承插管件_不锈钢承插管件_锻钢高压管件-温州科正阀门管件有限公司 | 压缩空气检测_气体_水质找上海京工-服务专业、价格合理 | 【MBA备考网】-2024年工商管理硕士MBA院校/报考条件/培训/考试科目/提前面试/考试/学费-MBA备考网 | 吸污车_吸粪车_抽粪车_电动三轮吸粪车_真空吸污车_高压清洗吸污车-远大汽车制造有限公司 | 废气处理_废气处理设备_工业废气处理_江苏龙泰环保设备制造有限公司 | 艺术涂料|木纹漆施工|稻草漆厂家|马来漆|石桦奴|水泥漆|选加河南天工涂料 | UV-1800紫外光度计-紫外可见光度计厂家-翱艺仪器(上海)有限公司 |