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", ""].
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!