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

How to clear bom header in php

藏色散人
藏色散人Original
2021-09-17 11:39:392898browse

How to clear the BOM header in php: 1. Use "trim($result, "\xEF\xBB\xBF");" to remove the BOM header; 2. Use "@iconv("GBK", " UTF-8//IGNORE", $result);" method to remove the BOM header.

How to clear bom header in php

The operating environment of this article: Windows7 system, PHP7.1 version, Dell G3 computer

How to clear the BOM header in php?

PHP method to remove BOM header

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 the BOM header issue at the beginning of its design, so problems are prone to occur 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 one that is more subtle:

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

Recommended study: "PHP Video Tutorial"

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