Created
December 28, 2012 21:56
-
-
Save claudiosanches/4402309 to your computer and use it in GitHub Desktop.
BOM Finder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 $WIN = 1; | |
$WIN = 0; | |
// 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(); | |
RecursiveFolder( $HOME ); | |
echo '<h2>These files have UTF8 BOM:</h2><p class="found">'; | |
foreach ( $BOMBED as $utf ) { echo $utf ."<br />\n"; } | |
echo '</p>'; | |
// Recursive finder | |
function RecursiveFolder( $sHOME ) { | |
global $BOMBED, $WIN; | |
$win32 = ( $WIN == 1 ) ? "\\" : "/"; | |
$folder = dir( $sHOME ); | |
$foundfolders = array(); | |
while ( $file = $folder->read() ) { | |
if ( $file != "." and $file != ".." ) { | |
if ( filetype( $sHOME . $win32 . $file ) == "dir" ) { | |
$foundfolders[count( $foundfolders )] = $sHOME . $win32 . $file; | |
} else { | |
$BOM = SearchBOM( file_get_contents( $sHOME . $win32 . $file ) ); | |
if ( $BOM ) $BOMBED[count( $BOMBED )] = $sHOME . $win32 . $file; | |
} | |
} | |
} | |
$folder->close(); | |
if ( count( $foundfolders ) > 0 ) { | |
foreach ( $foundfolders as $folder ) { | |
RecursiveFolder( $folder, $win32 ); | |
} | |
} | |
} | |
// Searching for BOM in files | |
function SearchBOM( $string ) { | |
if ( substr( $string, 0, 3 ) == pack( "CCC", 0xef, 0xbb, 0xbf ) ) return true; | |
return false; | |
} | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment