The json_decode function cannot be resolved due to BOM less thanfeffgreater and than encoding encountered in PHP

  • 2021-07-07 06:39:42
  • OfStack

Yesterday, my colleague encountered a strange problem, that is, the following code could not pass JSON verification and could not be parsed by json_decode function of PHP.


[
    {
        "title": "",
        "pinyin": ""
    }
]

It may be smart that you have guessed that it contains special characters that you don't see. Check it under vim:

[
    {
        <feff>"title": "",
        "pinyin": ""
    }
]

Found 1 character before "title" < feff > If you have known about BOM before, you should know that this special character is BOM. For its introduction, please refer to another article: String coding, garbled code, BOM and other issues in computers.


Under Linux, use the xxd command to view the 106-ary system of the file contents:


0000000: 5b 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20  [.    {.       
0000010: ef bb bf 22 74 69 74 6c 65 22 3a 20 22 22 2c 0a  ..."title": "",.
0000020: 20 20 20 20 20 20 20 20 22 70 69 6e 79 69 6e 22          "pinyin"
0000030: 3a 20 22 22 0a 20 20 20 20 7d 0a 5d 0a           : "".    }.].

You can see that the special character 106 before "title" just now is: ef bb bf, which is BOM marked UTF-8. BOM has the following meanings:

Beginning 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 00 00     UTF-32/UCS-4, little endian.
00 00 FE FF     UTF-32/UCS-4, big-endia

It is easy to find the problem and solve it. If you find and delete BOM, you will find OK. The related commands of BOM under linux are as follows:

BOM Operation of VIM


# Add BOM
:set bomb
# Delete BOM
:set nobomb
# Query BOM
:set bomb?

Find BOM in UTF-8 encoding

grep -I -r -l $'\xEF\xBB\xBF' /path

You can also disable the submission of BOM in the hook of svn (the following code comes from the network and is not verified)

#!/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, remind everyone that it is best not to use Notepad to automatically add BOM editor to modify the code under wowdows, which is easy to cause 1 problems.


Related articles: