Created
February 21, 2020 01:49
-
-
Save rlorenzo/13b4e58fc7cc47ca457a54ca8ab90c29 to your computer and use it in GitHub Desktop.
Finds the BOM character in a given directory. Run in command line as a user with ability to edit the files
This file contains hidden or 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 | |
// From: https://stackoverflow.com/a/9773452/6001 | |
// Tell me the root folder path. | |
// You can also try this one | |
// $HOME = $_SERVER["DOCUMENT_ROOT"]; | |
// Or this | |
// dirname(__FILE__) | |
// This uses a ton of memory if you have lots of files. | |
ini_set('memory_limit', '2G'); | |
$HOME = dirname(__FILE__); | |
// Is this a Windows host ? If it is, change this line to $WIN = 1; | |
$WIN = 0; | |
// That's all I need | |
$BOMBED = array(); | |
RecursiveFolder($HOME); | |
echo "These files had UTF8 BOM, but i cleaned them:\n"; | |
foreach ($BOMBED as $utf) { echo $utf ."\n"; } | |
// 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 { | |
$content = file_get_contents($sHOME . $win32 . $file); | |
$BOM = SearchBOM($content); | |
if ($BOM) { | |
$BOMBED[count($BOMBED)] = $sHOME . $win32 . $file; | |
// Remove first three chars from the file | |
$content = substr($content,3); | |
// Write to file | |
file_put_contents($sHOME . $win32 . $file, $content); | |
} | |
} | |
} | |
} | |
$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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment