Home > Backend Development > PHP Tutorial > How to Reliably Remove UTF-8 BOM Sequences from PHP Output?

How to Reliably Remove UTF-8 BOM Sequences from PHP Output?

Mary-Kate Olsen
Release: 2024-12-08 08:30:12
Original
670 people have browsed it

How to Reliably Remove UTF-8 BOM Sequences from PHP Output?

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

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

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!

source:php.cn
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template