Home  >  Article  >  Backend Development  >  How to clear bom in php

How to clear bom in php

coldplay.xixi
coldplay.xixiOriginal
2020-08-05 14:29:122944browse

php method to clear BOM: 1. Use the trim function to remove, the code is [$result = trim($result, "\xEF\xBB\xBF")]; 2. Use the iconv function to remove, the code is [$result = @iconv("UTF-8", ""].

How to clear bom in php

php method to clear BOM:

The BOM header is UTF-8 to tell the editor: I am UTF8 encoded. Its encoding is \xEF\xBB\xBF

but PHP did not consider it at the beginning of the design The problem is the BOM header, so it is easy to have problems when 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.

I tried two ways to remove it:

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

There is another way:

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

Related learning recommendations: php graphic tutorial

The above is the detailed content of How to clear bom 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