Detecting a BOM

--0016364c6dff8fd98b047d096e0b
Content-Type: text/plain; charset=ISO-8859-1

Hi,

is there any way to detect BOM in a file? I'm writing a parser which outputs
a number line if there's an error in it and its content. Every time it
parser a file saved in UTF-8 with BOM it outputs those three magic letters.
Can I pre-check for it and skip it? Or convert somehow?

Greetings,
Leszek Stachowski

--0016364c6dff8fd98b047d096e0b--
Leszek Stachowski [ Mi, 13 Januar 2010 11:42 ] [ ID #2028692 ]

Re: Detecting a BOM

On Wed, Jan 13, 2010 at 11:42, Leszek Stachowski <shazarre [at] gmail.com> wrote:
> Hi,
>
> is there any way to detect BOM in a file? I'm writing a parser which outputs
> a number line if there's an error in it and its content. Every time it
> parser a file saved in UTF-8 with BOM it outputs those three magic letters.
> Can I pre-check for it and skip it? Or convert somehow?
>
> Greetings,
> Leszek Stachowski

The UTF-8 byte order mark is represented by the hexadecimal character
sequence EF BB BF. You can use something like this to detect a BOM:

if (substr($source, 0, 3) == pack('CCC', 0xEF, 0xBB, 0xBF)) {
// has bom
}

--
Daniel Egeberg

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Daniel Egeberg [ Mi, 13 Januar 2010 19:49 ] [ ID #2028709 ]
PHP » gmane.comp.php.general » Detecting a BOM

Vorheriges Thema: What server am I authenticating to?
Nächstes Thema: Clean PHP 5.2.12 Build Core Dumping