Created
September 7, 2017 01:09
-
-
Save liuxd/5c223d6851f1442b4ac6e0974baa6f5c to your computer and use it in GitHub Desktop.
json_prepare.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * json_decode() 如果返回 'null',说明触犯了以下规则: | |
| * 1. 包含非UTF-8编码的字符。 | |
| * 2. 在最后元素有逗号。 | |
| * 3. 使用单引号包含元素的值。 | |
| * 4. 包含了`ASCII`码的`0~31`代表的不可见符号。 | |
| * 5. 包含了微软编辑器特有的`BOM`头。 | |
| * | |
| * 关于第4、5点问题,可以用以下函数处理: | |
| */ | |
| function json_prepare($str) | |
| { | |
| $aTobeReplaced = []; | |
| foreach (range(0, 31) as $v) { | |
| $aTobeReplaced[] = chr($v); | |
| } | |
| $str_handled = str_replace($aTobeReplaced, '', $str); | |
| $result = trim($str_handled,chr(239).chr(187).chr(191)); | |
| return $result; | |
| } | |
| // 另外,`json_last_error()`这个函数可以帮助定位问题的原因。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment