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
Mary-Kate OlsenOriginal
2024-12-08 08:30:12682browse

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;
}

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!

Statement:
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