Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem that the json_decode function cannot be parsed due to the BOM and & lt;feff> codes encountered in PHP

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to solve the problem that the json_decode function cannot be parsed due to BOM and coding in PHP". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Yesterday, my colleague encountered a strange problem, that is, the following code cannot pass the JSON check, nor can it be parsed by PHP's json_decode function.

The copy code is as follows:

[

{

"title": ""

"pinyin":

}

]

You may be smart enough to guess that it contains unseen special characters, check it out under vim:

The copy code is as follows:

[

{

"title":

"pinyin":

}

]

It is found that there is a character before "title". If you have known BOM before, you should know that this special character is BOM. About its introduction, you can refer to another article: string coding, garbled code, BOM and other problems in the computer.

Under Linux, use the xxd command to view the hexadecimal contents of the file:

The copy code is as follows:

0000000: 5b 0a 20 20 20 7b 0a 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 22 70 69 6e 79 69 6e 22 "pinyin"

0000030: 3a 20 22 22 0a 20 20 20 7d 0a 5d 0a: ". }.].

You can see that the special character in front of the "title" just now is hexadecimal: ef bb bf, which is the BOM marking UTF-8. What BOM means is as follows:

The copy code is as follows:

First 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

Find that it is easy to solve the problem. Find and delete BOM and OK. The commands related to BOM under linux are as follows:

BOM operation of VIM

The copy code is as follows:

# add BOM

: set bomb

# Delete BOM

: set nobomb

# query BOM

: set bomb?

Find BOM in UTF-8 encoding

The copy code is as follows:

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)

The copy 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, 0xbfeld; then

Echo "BOM!" 1 > & 2

Exit 1

Fi

Done

This is the end of the content of "how to solve the problem that the json_decode function cannot be parsed due to BOM and coding in PHP". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report