The json_decode function cannot be parsed due to BOM, & amp; lt; feff & amp; gt; encoding in PHP. Yesterday, my colleague encountered a strange problem: the following code cannot pass JSON verification or parse through the json_decode function of PHP. Copy the code as follows: [{title: Yesterday, my colleague encountered a strange problem: the following code cannot pass JSON verification or parse through the json_decode function of PHP.
The code is as follows:
[
{
Remark "title ":"",
"Pinyin ":""
}
]
It may be wise that you have guessed that it contains special characters that you cannot see. in vim, view:
The code is as follows:
[
{
"Title ":"",
"Pinyin ":""
}
]
It is found that there is a character before "title" If you have learned about BOM before, you should know that this special character is BOM. For more information, refer to another article: detailed explanations of string encoding, garbled code, BOM, and other issues in the computer.
In Linux, run the xxd command to view the hexadecimal format of the file content:
The code is as follows:
0000000: 5b 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 [.{.
0000010: ef bb bf 22 74 69 74 6c 65 22 3a 20 22 2c 0a... "title ":"",.
0000020: 20 20 20 20 20 20 22 70 69 6e 79 69 6e 22 "pinyin"
0000030: 3a 20 22 22 0a 20 20 20 7d 0a 5d 0a: "".}.].
We can see that the special character hex before "title" is ef bb bf, which is the BOM marking the UTF-8. The meaning of BOM is as follows:
The code is as follows:
Character Set/encoding
Ef bb bf UTF-8
Fe ff UTF-16/UCS-2, little endian (UTF-16LE)
Ff fe UTF-16/UCS-2, big endian (UTF-16BE)
Ff fe 00 00 UTF-32/UCS-4, little endian.
00 00 fe ff UTF-32/UCS-4, big-endia
It is easy to solve the problem, and it is OK to find and delete the BOM. The BOM-related commands in linux include:
Vim bom operations
The code is as follows:
# Add BOM
: Set bomb
# Delete BOM
: Set nobomb
# Query BOM
: Set bomb?
Find BOM in UTF-8 encoding
The code is as follows:
Grep-I-r-l $ '\ xEF \ xBB \ xBF'/path
You can also disable submitting BOM in svn hooks (the following code is from the network and is not verified)
The code is as follows:
#! /Bin/sh
REPOS = "$1"
TXN = "$2"
SVNLOOK =/usr/bin/svnlook
FILES = '$ SVNLOOK changed-t "$ TXN" "$ REPOS" | awk {'print $2 '}'
For FILE in $ FILES; do
CONTENT = '$ SVNLOOK cat-t "$ TXN" "$ REPOS" "$ FILE "'
If echo $ CONTENT | head-c 3 | xxd-I | grep-q '0xef, 0xbb, 0xbf '; then
Echo "BOM! "1> & 2
Exit 1
Fi
Done
Finally, it is recommended that you do not use notepad in wowdows to automatically add the BOM Editor to modify the code, which may cause some problems.
Bytes. The code is as follows: [{"title ":"",...