How to remove a Byte Order Mark

A byte order mark is supposed to begin a unicode plain text file. You can read about this at: http://msdn2.microsoft.com/en-us/library/ms776429%28VS.85%29.aspx »

I ran into this while trying to parse an RSS feed. I believe xml_parse_insto_struct() was throwing the error. It can actually through you off, because you can't readily see the byte order mark and the php errors don't give you a hint of the problem.

I found this nice function on http://docs.php.net » that will remove the byte order mark. There's also a function to add a byte order mark.

Remove Byte Order Mark

<?php
function removeBOM($str="")
{
        if(substr($str,0,3) == pack("CCC",0xef,0xbb,0xbf))
        {
                $str=substr($str,3);
        }

        return $str;
}
?>

Write UTF-8 file with byte order mark

<?php
function writeUTF8File($filename,$content) {
        $dhandle=fopen($filename,"w");
        # Now UTF-8 - Add byte order mark
       fwrite($dhandle, pack("CCC",0xef,0xbb,0xbf));
        fwrite($dhandle,$content);
        fclose($dhandle);
}
?>

http://docs.php.net/manual/de/function.utf8-encode.php »

Page Comments (Click to edit)

Design by N.Design Studio, adapted by solidGone.org (version 1.0.0)
Powered by pmwiki-2.2.0-beta65