Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jdlx
Created May 11, 2012 15:39
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save jdlx/2660507 to your computer and use it in GitHub Desktop.
Save jdlx/2660507 to your computer and use it in GitHub Desktop.
PHP script to recursively check php files for BOM (ByteOrderMark)
<?php
/**
* @author Atanas Vasilev
* @link http://pastebin.com/dHbqjUNy
* @see http://www.dotvoid.com/2010/04/detecting-utf-bom-byte-order-mark/
* @version 1.1
*/
// SETTINGS
////////////////////////////////////////////////////////////////////////////////
$check_extensions = array('php');
// MAIN
////////////////////////////////////////////////////////////////////////////////
define('STR_BOM', "\xEF\xBB\xBF");
$file = null;
$directory = getcwd();
$rit = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory), RecursiveIteratorIterator::CHILD_FIRST);
echo '<h1>BOM Check</h1>';
try
{
foreach ($rit as $file)
{
if ($file->isFile())
{
$path_parts = pathinfo($file->getRealPath());
if (isset($path_parts['extension']) && in_array($path_parts['extension'],$check_extensions))
{
$object = new SplFileObject($file->getRealPath());
if (false !== strpos($object->getCurrentLine(), STR_BOM))
{
echo $file->getRealPath().'<br />';
}
}
}
}
}
catch (Exception $e)
{
die ('Exception caught: '. $e->getMessage());
}
?>
@aeberhard
Copy link

Danke Jan, hat mir soeben ein paar graue Haare erspart :)

Copy link

ghost commented Jul 24, 2015

when run it it just says BOM Check

@azett
Copy link

azett commented Jun 3, 2021

2021 and still works like a charm. Thanks for sharing - danke :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment