The json_decode function cannot be parsed due to BOM and & lt; feff & gt; encoding in PHP.

Source: Internet
Author: User

Yesterday, my colleague encountered a strange problem: the following code cannot pass JSON verification or parse through the json_decode function of PHP.
Copy codeThe 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:
Copy codeThe Code is as follows:
[
{
<Feff> "title ":"",
"Pinyin ":""
}
]
It is found that there is a character <feff> in front of the title. If you have learned about BOM before, you should know that this special character is BOM. For more information, see another article: detailed explanation 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:
Copy codeThe 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:
Copy codeThe 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
Copy codeThe Code is as follows:
# Add BOM
: Set bomb
# Delete BOM
: Set nobomb
# Query BOM
: Set bomb?

Find BOM in UTF-8 Encoding
Copy codeThe 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)
Copy codeThe 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.