Tuesday, 15 June 2010

Byte Order mark (BOM) woes

PHP seems to suffer from the BOM problem on windows more than most - here is some code I found that will look through your files and tell you which ones have the evil mark...

http://pastebin.com/dHbqjUNy


define('STR_BOM', "\xEF\xBB\xBF");
$file = null;
$directory = getcwd();

$rit = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory), RecursiveIteratorIterator::CHILD_FIRST);
try {
foreach ($rit as $file) {
if ($file->isFile()) {
$path_parts = pathinfo($file->getRealPath());

if ('php' == $path_parts['extension']) {
$object = new SplFileObject($file->getRealPath());

if (false !== strpos($object->getCurrentLine(), STR_BOM)) {
print $file->getRealPath()."\n";
}
}
}
}

} catch (Exception $e) {
die ('Exception caught: '. $e->getMessage());
}



remember to wrap it in php tags - they are being removed by blogger so i can't show them.

No comments: