Skip to content

Instantly share code, notes, and snippets.

@Johann150
Forked from Xhamps/remove-bom.php
Last active January 27, 2018 21:20
Show Gist options
  • Save Johann150/dabf1d27851585cbd7f1d014385d0790 to your computer and use it in GitHub Desktop.
Save Johann150/dabf1d27851585cbd7f1d014385d0790 to your computer and use it in GitHub Desktop.
Remove Bom
<?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);
}
@Johann150
Copy link
Author

I tried to minimize the original and make it more reliable (was probably written a few PHP versions back)

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