How to remove bom header in php

藏色散人
Release: 2023-03-03 15:06:01
Original
2040 people have browsed it

How to remove the bom header in php: 1. Use the "json_decode($result, true)" method to achieve removal; 2. Use "@iconv("UTF-8", "GBK//IGNORE", $ result);" to remove the BOM header.

How to remove bom header in php

Recommendation: "PHP Video Tutorial"

PHP method to remove BOM header

But PHP did not consider the BOM header problem at the beginning of the design, so it is easy to have problems during encoding and decoding

For example, the problem encountered today, json_decode , when the decoded string has a BOM header, json_decode fails to parse and returns NULL. (Why not automatically detect and remove the BOM header... small complaint)

I tried two ways to remove it:

$result = trim($result, "\xEF\xBB\xBF");
print_r(json_decode($result, true));
exit;
Copy after login

There is another kind that is more reserved:

$result = @iconv("UTF-8", "GBK//IGNORE", $result);
$result = @iconv("GBK", "UTF-8//IGNORE", $result);
 
print_r(json_decode($result, true));
exit;
Copy after login

The above is the detailed content of How to remove bom header in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template