Home  >  Article  >  Backend Development  >  How to remove bom header in php

How to remove bom header in php

藏色散人
藏色散人Original
2020-07-31 09:10:141966browse

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;

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;

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!

Statement:
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