Skip to content

Instantly share code, notes, and snippets.

@claudiosanches
Last active November 24, 2015 18:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save claudiosanches/2347962 to your computer and use it in GitHub Desktop.
Save claudiosanches/2347962 to your computer and use it in GitHub Desktop.
UTF8 BOM finder
<?php
// Tell me the root folder path.
// You can also try this one
// $home = $_SERVER['DOCUMENT_ROOT'];
// Or this
// dirname( __FILE__ )
$home = dirname( __FILE__ );
// Is this a Windows host ? If it is, change this line to $windows = true;
$windows = false;
// That's all I need
?><!DOCTYPE html>
<html dir="ltr" lang="pt-BR">
<head>
<meta charset="UTF-8" />
<title>UTF8 BOM FINDER</title>
<style>
body {
font-size: 10px;
font-family: Arial, Helvetica, sans-serif;
background: #fff;
color: #000;
}
.found {
color: #F30;
font-size: 14px;
font-weight: bold;
}
</style>
</head>
<body>
<?php
$bombed = array();
recursive_folder( $home );
echo '<h2>These files have UTF8 BOM:</h2>';
echo '<p class="found">';
foreach ( $bombed as $utf ) {
echo "$utf<br />\n";
}
echo '</p>';
// Recursive finder.
function recursive_folder( $shome ) {
global $bombed, $windows;
$win32 = ( $windows ) ? "\\" : "/";
$folder = dir( $shome );
$foundfolders = array();
while ( $file = $folder->read() ) {
if ( '.' != $file && '..' != $file ) {
if ( 'dir' == filetype( $shome . $win32 . $file ) ) {
$foundfolders[ count( $foundfolders ) ] = $shome . $win32 . $file;
} else {
$bom = search_bom( file_get_contents( $shome . $win32 . $file ) );
if ( $bom ) {
$bombed[ count( $bombed ) ] = $shome . $win32 . $file;
}
}
}
}
$folder->close();
if ( 0 < count( $foundfolders ) ) {
foreach ($foundfolders as $folder) {
recursive_folder($folder, $win32);
}
}
}
// Searching for BOM in files.
function search_bom( $string ) {
if ( substr( $string, 0, 3 ) == pack( 'CCC', 0xef, 0xbb, 0xbf ) ) {
return true;
}
return false;
}
?>
</body>
</html>
@giovannipds
Copy link

Does it work? It didn't work for me, or maybe I'm using it wrong, somehow...

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