Home > Backend Development > PHP Problem > How to clear bom in php

How to clear bom in php

coldplay.xixi
Release: 2023-03-03 20:32:01
Original
3050 people have browsed it

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;
Copy after login

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;
Copy after login

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!

Related labels:
php
source:php如何清除bom
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