-
-
Save Johann150/dabf1d27851585cbd7f1d014385d0790 to your computer and use it in GitHub Desktop.
Remove Bom
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 | |
// root directory in which to start searching | |
$HOME=$_SERVER["DOCUMENT_ROOT"]; | |
// use alternatively: | |
// $HOME=dirname(__FILE__); | |
// $HOME=dirname(__FILE__); | |
header("Content-Type: text/plain"); | |
echo "BOM removed from files:\n"; | |
RecursiveFolder($HOME); | |
// Recursive finder | |
function RecursiveFolder($sHOME){ | |
$folder=dir($sHOME); | |
$foundfolders=array(); | |
while($file=$folder->read()){ | |
if($file=="."||$file=="..") | |
continue; | |
if(is_dir($sHOME.DIRECTORY_SEPARATOR.$file)){ | |
$foundfolders[]=$sHOME.DIRECTORY_SEPARATOR.$file; | |
}else if(substr(mime_content_type($file),0,4)=="text"){ | |
$t=file_get_contents($sHOME.DIRECTORY_SEPARATOR.$file); | |
if(SearchBOM($content)){ | |
echo $sHOME.DIRECTORY_SEPARATOR.$file."\n"; | |
// Remove first three chars from the file | |
$t=substr($t,3); | |
// Write to file | |
file_put_contents($sHOME.DIRECTORY_SEPARATOR.$file,$t); | |
} | |
} | |
} | |
$folder->close(); | |
if(count($foundfolders)>0){ | |
foreach($foundfolders as $folder){ | |
RecursiveFolder($folder); | |
} | |
} | |
} | |
// Searching for BOM in files | |
function SearchBOM($s){ | |
return substr($s,0,3)==pack("CCC",0xef,0xbb,0xbf); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tried to minimize the original and make it more reliable (was probably written a few PHP versions back)