How to Effectively Remove Multiple UTF-8 BOM Sequences
When encountering issues with displaying raw HTML output from PHP template files, it's essential to address the presence of UTF-8 BOM (Byte Order Mark) sequences. This issue can manifest as Firefox being unable to accept the output.
The following PHP code snippet attempts to remove BOM sequences but may not be fully effective:
function fetch($name) { // ... if (substr($t, 0, 3) == b'\xef\xbb\xbf') { $t = substr($t, 3); } return $t; }
To ensure the complete removal of all BOM sequences, consider using the following more robust approach:
function remove_utf8_bom($text) { $bom = pack('H*','EFBBBF'); $text = preg_replace("/^$bom/", '', $text); return $text; }
This function utilizes a regular expression (preg_replace) to match and remove any instance of the UTF-8 BOM sequence at the beginning of the string (/^$bom/). By ensuring the removal of all BOMs, the output should be readily acceptable by Firefox and other browsers.
The above is the detailed content of How to Reliably Remove UTF-8 BOM Sequences from PHP Output?. For more information, please follow other related articles on the PHP Chinese website!