Eliminating Multiple UTF-8 BOM Sequences
In response to the issue of outputting raw HTML from template files using PHP5, where the removal of the Byte Order Mark (BOM) was not resolving Firefox compatibility, a more comprehensive solution has been identified.
When attempting to remove the BOM, the code provided:
if (substr($t, 0, 3) == b'\xef\xbb\xbf') { $t = substr($t, 3); }
only addresses the removal of a single BOM sequence. However, to ensure compatibility with Firefox, it is necessary to eliminate all instances of the BOM.
Revised Code for BOM Removal
To remove multiple UTF-8 BOM sequences, the following code is recommended:
function remove_utf8_bom($text) { $bom = pack('H*','EFBBBF'); $text = preg_replace("/^$bom/", '', $text); return $text; }
Explanation of the Code
By implementing this code, the template files will be rendered correctly, resolving the compatibility issue with Firefox.
The above is the detailed content of How Can I Reliably Remove Multiple UTF-8 BOM Sequences from PHP Template Files?. For more information, please follow other related articles on the PHP Chinese website!