Current Location: Home> Latest Articles> How to Remove BOM (Byte Order Mark) in PHP Code

How to Remove BOM (Byte Order Mark) in PHP Code

gitbox 2025-06-24

How to Remove BOM (Byte Order Mark) in PHP Code

In PHP code, BOM (Byte Order Mark) can sometimes cause issues. BOM is a special character that adds a few bytes at the beginning of a file to indicate the text's character encoding. In PHP projects, the presence of BOM can lead to output errors like garbled text or issues with string manipulation. It's important to know how to remove BOM to avoid these issues.

Impacts of BOM Issues

The presence of BOM can lead to several problems, especially when outputting file content to the browser, which may result in garbled text or unexpected errors. BOM is usually added automatically by text editors, but it can be detected and removed in PHP code.

How to Detect BOM in PHP Files

We can use the following code to detect whether a file contains BOM:


function hasBom($filename) {
    $file = fopen($filename, "r");
    $bom = fread($file, 3);
    fclose($file);
    return $bom === "\xEF\xBB\xBF";
}
if (hasBom("example.php")) {
    echo "The file contains BOM";
} else {
    echo "The file does not contain BOM";
}
        

This code reads the first 3 bytes of the file and checks if they match the BOM byte sequence "\xEF\xBB\xBF". If they match, the file contains BOM.

Two Methods to Remove BOM

Method 1: Re-save the File

The simplest method is to open the file containing BOM, save it as a new file, and then replace the original file with the new one. This ensures the new file does not contain BOM.

Here’s an example of how to re-save a file in PHP and remove the BOM:


function removeBom($filename) {
    $content = file_get_contents($filename);
    file_put_contents($filename, substr($content, 3));
}
removeBom("example.php");
        

This code reads the file content, removes the first three bytes (the BOM), and writes the modified content back into the file, thereby removing the BOM.

Method 2: Using a Custom Function

In addition to re-saving the file, you can also use a custom PHP function to remove the BOM from a string. This method works by checking for BOM in the string and removing it if found.


function utf8_encode_bom($string) {
    if (substr($string, 0, 3) == "\xEF\xBB\xBF") {
        return substr($string, 3);
    }
    return $string;
}
$originalString = "Example String with BOM";
$cleanString = utf8_encode_bom($originalString);
echo $cleanString;
        

This code checks the first 3 bytes of the string for BOM. If BOM is found, it removes it and returns the cleaned string.

Conclusion

Removing BOM from PHP code is a common issue, especially when dealing with PHP files from different text editors. By detecting the BOM and using methods such as re-saving the file or using custom functions, we can easily remove BOM and prevent encoding issues. This ensures your PHP code runs smoothly without any unexpected characters or errors.

Always remember to back up your original files before making any changes to avoid accidental loss of data.