Yesterday, a colleague encountered a strange problem, that is, the following code, can not pass the JSON checksum, and can not be resolved through the PHP json_decode function.
Copy Code code as follows:
[
{
"title": "",
"Pinyin": ""
}
]
Probably smart you've guessed it contains a special character that you don't see, and you can view it under vim:
Copy Code code as follows:
[
{
<feff> "title": "",
"Pinyin": ""
}
]
Found in the "title" preceded by a character <feff>, if you know before the BOM, you should know that this special character is a BOM, about its introduction can refer to another article: the computer string code, garbled, BOM and so on.
Under Linux, view the contents of a file in 16 through the XXD command:
Copy Code code as follows:
0000000:5B 0a 7b 0a 20 20 20 20 20 20 20 20 [. {.
0000010:EF BB BF-6c 3a 2c 0a ... "title": "",.
0000020:20 6e "Pinyin" to 6e "a".
0000030:3A 0a 7d 0a 5d 0a: "". }.].
Can see just that "title" before the special character hexadecimal: the EF BB BF, is the mark UTF-8 BOM. The meaning of the BOM is as follows:
Copy Code code as follows:
Opening byte charset/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 utf-32/ucs-4, little endian.
FE FF utf-32/ucs-4, Big-endia
Find the problem solved is very easy, find delete BOM ok, Linux under the BOM related commands are:
Vim's BOM operation
Copy Code code as follows:
#添加BOM
: Set Bomb
#删除BOM
: Set Nobomb
#查询BOM
: Set bomb?
Find BOM in UTF-8 encoding
Copy Code code as follows:
Grep-i-r-l $ ' \xef\xbb\xbf '/path
You can also disallow the submission of the BOM in SVN hooks (the following code comes from the network, not verified)
Copy Code code as follows:
#!/bin/sh
Repos= "$"
Txn= "$"
Svnlook=/usr/bin/svnlook
files= ' $SVNLOOK changed-t "$TXN" "$REPOS" | awk {' Print $ '} '
For FILE in $FILES; Todo
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 remind everyone in the wowdows under the best not to use Notepad and so will automatically add the BOM editor to modify the code, easy to cause some problems.