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

<legend id='rfiyB'><style id='rfiyB'><dir id='rfiyB'><q id='rfiyB'></q></dir></style></legend>

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

  1. <tfoot id='rfiyB'></tfoot>
    1. <small id='rfiyB'></small><noframes id='rfiyB'>

      如何防止 json_encode() 刪除包含無效字符的字符串

      How to keep json_encode() from dropping strings with invalid characters(如何防止 json_encode() 刪除包含無效字符的字符串)
        <bdo id='4Xz4b'></bdo><ul id='4Xz4b'></ul>

        <small id='4Xz4b'></small><noframes id='4Xz4b'>

          <tbody id='4Xz4b'></tbody>

          <tfoot id='4Xz4b'></tfoot>
          <legend id='4Xz4b'><style id='4Xz4b'><dir id='4Xz4b'><q id='4Xz4b'></q></dir></style></legend>
            <i id='4Xz4b'><tr id='4Xz4b'><dt id='4Xz4b'><q id='4Xz4b'><span id='4Xz4b'><b id='4Xz4b'><form id='4Xz4b'><ins id='4Xz4b'></ins><ul id='4Xz4b'></ul><sub id='4Xz4b'></sub></form><legend id='4Xz4b'></legend><bdo id='4Xz4b'><pre id='4Xz4b'><center id='4Xz4b'></center></pre></bdo></b><th id='4Xz4b'></th></span></q></dt></tr></i><div class="mag0kwq" id='4Xz4b'><tfoot id='4Xz4b'></tfoot><dl id='4Xz4b'><fieldset id='4Xz4b'></fieldset></dl></div>

                本文介紹了如何防止 json_encode() 刪除包含無效字符的字符串的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                有沒有辦法防止 json_encode() 為包含無效(非 UTF-8)字符的字符串返回 null?

                Is there a way to keep json_encode() from returning null for a string that contains an invalid (non-UTF-8) character?

                在復雜的系統中調試可能會很麻煩.實際看到無效字符或至少將其省略會更合適.就目前而言,json_encode() 將靜默刪除整個字符串.

                It can be a pain in the ass to debug in a complex system. It would be much more fitting to actually see the invalid character, or at least have it omitted. As it stands, json_encode() will silently drop the entire string.

                示例(UTF-8):

                $string = 
                  array(utf8_decode("Düsseldorf"), // Deliberately produce broken string
                        "Washington",
                        "Nairobi"); 
                
                print_r(json_encode($string));
                

                結果

                [null,"Washington","Nairobi"]
                

                想要的結果:

                ["D?sseldorf","Washington","Nairobi"]
                

                注意:我希望讓損壞的字符串在 json_encode() 中起作用.我正在尋找更容易診斷編碼錯誤的方法.null 字符串對此沒有幫助.

                Note: I am not looking to make broken strings work in json_encode(). I am looking for ways to make it easier to diagnose encoding errors. A null string isn't helpful for that.

                推薦答案

                php 確實會嘗試拋出錯誤,但僅當您關閉 display_errors 時.這很奇怪,因為 display_errors 設置僅用于控制是否將錯誤打印到標準輸出,而不是是否觸發錯誤.我想強調的是,當您打開 display_errors 時,即使您可能會看到各種其他 php 錯誤,php 不僅會隱藏此錯誤,它甚至不會觸發它.這意味著它不會出現在任何錯誤日志中,也不會調用任何自定義的 error_handlers.錯誤永遠不會發生.

                php does try to spew an error, but only if you turn display_errors off. This is odd because the display_errors setting is only meant to control whether or not errors are printed to standard output, not whether or not an error is triggered. I want to emphasize that when you have display_errors on, even though you may see all kinds of other php errors, php doesn't just hide this error, it will not even trigger it. That means it will not show up in any error logs, nor will any custom error_handlers get called. The error just never occurs.

                這里有一些代碼可以證明這一點:

                Here's some code that demonstrates this:

                error_reporting(-1);//report all errors
                $invalid_utf8_char = chr(193);
                
                ini_set('display_errors', 1);//display errors to standard output
                var_dump(json_encode($invalid_utf8_char));
                var_dump(error_get_last());//nothing
                
                ini_set('display_errors', 0);//do not display errors to standard output
                var_dump(json_encode($invalid_utf8_char));
                var_dump(error_get_last());// json_encode(): Invalid UTF-8 sequence in argument
                

                這種奇怪而不幸的行為與此錯誤有關 https://bugs.php.net/bug.php?id=47494 和其他一些,而且看起來永遠不會被修復.

                That bizarre and unfortunate behavior is related to this bug https://bugs.php.net/bug.php?id=47494 and a few others, and doesn't look like it will ever be fixed.

                解決方法:

                在將字符串傳遞給 json_encode 之前清理字符串可能是一個可行的解決方案.

                Cleaning the string before passing it to json_encode may be a workable solution.

                $stripped_of_invalid_utf8_chars_string = iconv('UTF-8', 'UTF-8//IGNORE', $orig_string);
                if ($stripped_of_invalid_utf8_chars_string !== $orig_string) {
                    // one or more chars were invalid, and so they were stripped out.
                    // if you need to know where in the string the first stripped character was, 
                    // then see http://stackoverflow.com/questions/7475437/find-first-character-that-is-different-between-two-strings
                }
                $json = json_encode($stripped_of_invalid_utf8_chars_string);
                

                http://php.net/manual/en/function.iconv.php

                說明書上說

                //IGNORE 靜默丟棄目標中的非法字符字符集.

                //IGNORE silently discards characters that are illegal in the target charset.

                所以首先刪除有問題的字符,理論上 json_encode() 不應該得到任何它會窒息和失敗的東西.我還沒有驗證帶有 //IGNORE 標志的 iconv 的輸出與有效 utf8 字符是什么的 json_encodes 概念完全兼容,所以買家要當心......因為可能存在邊緣情況仍然失敗.呃,我討厭字符集問題.

                So by first removing the problematic characters, in theory json_encode() shouldnt get anything it will choke on and fail with. I haven't verified that the output of iconv with the //IGNORE flag is perfectly compatible with json_encodes notion of what valid utf8 characters are, so buyer beware...as there may be edge cases where it still fails. ugh, I hate character set issues.

                編輯
                在 php 7.2+ 中,json_encode 似乎有一些新標志:JSON_INVALID_UTF8_IGNOREJSON_INVALID_UTF8_SUBSTITUTE
                目前還沒有太多文檔,但就目前而言,此測試應該可以幫助您了解預期行為:https://github.com/php/php-src/blob/master/ext/json/tests/json_encode_invalid_utf8.phpt

                Edit
                in php 7.2+, there seems to be some new flags for json_encode: JSON_INVALID_UTF8_IGNORE and JSON_INVALID_UTF8_SUBSTITUTE
                There's not much documentation yet, but for now, this test should help you understand expected behavior: https://github.com/php/php-src/blob/master/ext/json/tests/json_encode_invalid_utf8.phpt

                而且,在 php 7.3+ 中有新標志 JSON_THROW_ON_ERROR.參見 http://php.net/manual/en/class.jsonexception.php

                And, in php 7.3+ there's the new flag JSON_THROW_ON_ERROR. See http://php.net/manual/en/class.jsonexception.php

                這篇關于如何防止 json_encode() 刪除包含無效字符的字符串的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

                相關文檔推薦

                Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死鎖異常代碼?)
                PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滾動游標不起作用)
                PHP PDO ODBC connection(PHP PDO ODBC 連接)
                Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔術方法)
                php pdo get only one value from mysql; value that equals to variable(php pdo 只從 mysql 獲取一個值;等于變量的值)
                MSSQL PDO could not find driver(MSSQL PDO 找不到驅動程序)
                  <bdo id='r63o3'></bdo><ul id='r63o3'></ul>

                      <tfoot id='r63o3'></tfoot>

                        <small id='r63o3'></small><noframes id='r63o3'>

                          <tbody id='r63o3'></tbody>
                      1. <i id='r63o3'><tr id='r63o3'><dt id='r63o3'><q id='r63o3'><span id='r63o3'><b id='r63o3'><form id='r63o3'><ins id='r63o3'></ins><ul id='r63o3'></ul><sub id='r63o3'></sub></form><legend id='r63o3'></legend><bdo id='r63o3'><pre id='r63o3'><center id='r63o3'></center></pre></bdo></b><th id='r63o3'></th></span></q></dt></tr></i><div class="0awwswa" id='r63o3'><tfoot id='r63o3'></tfoot><dl id='r63o3'><fieldset id='r63o3'></fieldset></dl></div>
                      2. <legend id='r63o3'><style id='r63o3'><dir id='r63o3'><q id='r63o3'></q></dir></style></legend>
                        • 主站蜘蛛池模板: ERP企业管理系统永久免费版_在线ERP系统_OA办公_云版软件官网 | PC构件-PC预制构件-构件设计-建筑预制构件-PC构件厂-锦萧新材料科技(浙江)股份有限公司 | lcd条形屏-液晶长条屏-户外广告屏-条形智能显示屏-深圳市条形智能电子有限公司 | 杭州代理记账费用-公司注销需要多久-公司变更监事_杭州福道财务管理咨询有限公司 | PU树脂_水性聚氨酯树脂_聚氨酯固化剂_聚氨酯树脂厂家_宝景化工 | 考试试题_试卷及答案_诗词单词成语 - 优易学 | 贴片电容代理-三星电容-村田电容-风华电容-国巨电容-深圳市昂洋科技有限公司 | 隆众资讯-首页_大宗商品资讯_价格走势_市场行情 | 仓储货架_南京货架_钢制托盘_仓储笼_隔离网_环球零件盒_诺力液压车_货架-南京一品仓储设备制造公司 | atcc网站,sigma试剂价格,肿瘤细胞现货,人结肠癌细胞株购买-南京科佰生物 | 筒瓦厂家-仿古瓦-寺庙-古建琉璃瓦-宜兴市古典园林建筑陶瓷厂有限公司 | 曙光腾达官网-天津脚手架租赁-木板架出租-移动门式脚手架租赁「免费搭设」 | 紫外线老化试验箱_uv紫外线老化试验箱价格|型号|厂家-正航仪器设备 | 废气处理_废气处理设备_工业废气处理_江苏龙泰环保设备制造有限公司 | 金属切削液-脱水防锈油-电火花机油-抗磨液压油-深圳市雨辰宏业科技发展有限公司 | 深圳3D打印服务-3D打印加工-手板模型加工厂-悟空打印坊 | 深圳市超时尚职业培训学校,培训:月嫂,育婴,养老,家政;化妆,美容,美发,美甲. | 沈飞防静电地板__机房地板-深圳市沈飞防静电设备有限公司 | 诚暄电子公司首页-线路板打样,pcb线路板打样加工制作厂家 | 气动绞车,山东气动绞车,气动绞车厂家-烟台博海石油机械有限公司 气动隔膜泵厂家-温州永嘉定远泵阀有限公司 | LCD3D打印机|教育|桌面|光固化|FDM3D打印机|3D打印设备-广州造维科技有限公司 | 阿米巴企业经营-阿米巴咨询管理-阿米巴企业培训-广东键锋企业管理咨询有限公司 | 东莞工作服_东莞工作服定制_工衣订做_东莞厂服 | 产业规划_产业园区规划-产业投资选址及规划招商托管一体化服务商-中机院产业园区规划网 | 挤出熔体泵_高温熔体泵_熔体出料泵_郑州海科熔体泵有限公司 | 无水硫酸铝,硫酸铝厂家-淄博双赢新材料科技有限公司 | 盐水蒸发器,水洗盐设备,冷凝结晶切片机,转鼓切片机,絮凝剂加药系统-无锡瑞司恩机械有限公司 | 培训中心-翰香原香酥板栗饼加盟店总部-正宗板栗酥饼技术 | 工业车间焊接-整体|集中除尘设备-激光|等离子切割机配套除尘-粉尘烟尘净化治理厂家-山东美蓝环保科技有限公司 | 电动液压篮球架_圆管地埋式篮球架_移动平箱篮球架-强森体育 | 论文查重_免费论文查重_知网学术不端论文查重检测系统入口_论文查重软件 | 鹤壁创新仪器公司-全自动量热仪,定硫仪,煤炭测硫仪,灰熔点测定仪,快速自动测氢仪,工业分析仪,煤质化验仪器 | 鹤壁创新仪器公司-全自动量热仪,定硫仪,煤炭测硫仪,灰熔点测定仪,快速自动测氢仪,工业分析仪,煤质化验仪器 | 合肥制氮机_合肥空压机厂家_安徽真空泵-凯圣精机 | 重庆监控_电子围栏设备安装公司_门禁停车场管理系统-劲浪科技公司 | 聚氨酯保温钢管_聚氨酯直埋保温管道_聚氨酯发泡保温管厂家-沧州万荣防腐保温管道有限公司 | 电动葫芦|防爆钢丝绳电动葫芦|手拉葫芦-保定大力起重葫芦有限公司 | 磨煤机配件-高铬辊套-高铬衬板-立磨辊套-盐山县宏润电力设备有限公司 | 数码听觉统合训练系统-儿童感觉-早期言语评估与训练系统-北京鑫泰盛世科技发展有限公司 | 伶俐嫂培训学校_月嫂培训班在哪里报名学费是多少_月嫂免费政府培训中心推荐 | 消电检公司,消电检价格,北京消电检报告-北京设施检测公司-亿杰(北京)消防工程有限公司 |