PHP BOM Removal tool, PHP batch removal of BOM code
PHP batch removal of file BOM code function, Support folder batch removal. The specific PHP file code is as follows:
Header (' content-type:text/html; Charset=utf-8 ');
$auto =1;/* is set to 1 mark to detect BOM and remove, set to 0 mark only for BOM detection, do not remove */
$basedir = '. ';
$loop =true;//www.phpernote.com
Echo ' Currently looking for directory is: '. $basedir. ' The current setting is: ';
Echo ' (1) ', $loop? ' Check the current directory and subdirectories of the current directory ': ' Only for current directory detection ';
Echo ' (2) ', $auto? ' Detects the BOM of a BOM while removing the bill of documentation
': ' Only detect file BOM does not perform removal of BOM operation
';
Checkdir ($basedir, $loop);
function Checkdir ($basedir = ", $loop =true) {
$basedir =empty ($basedir)? '. ': $basedir;
if ($dh =opendir ($basedir)) {
while (($file =readdir ($DH))!==false) {
if ($file! = '. ') && $file! = ' ... ') {
if (!is_dir ($basedir. ' /'. $file)) {
echo ' file: '. $basedir. ' /'. $file. Checkbom ($basedir. ' /'. $file). '
';
}else{
if (! $loop) continue;
$dirname = $basedir. ' /'. $file;
Checkdir ($dirname);
}
}
}
Closedir ($DH);
}
}
function Checkbom ($filename) {
Global $auto;
$contents =file_get_contents ($filename);
$charset [1]=substr ($contents, 0, 1);
$charset [2]=substr ($contents, 1, 1);
$charset [3]=substr ($contents, 2, 1);
if (Ord ($charset [1]) ==239&&ord ($charset [2]) ==187&&ord ($charset [3]) ==191) {
if ($auto ==1) {
$rest =substr ($contents, 3);
Rewrite ($filename, $rest);
Return (' Find BOM and automatically remove ');
}else{
Return (' Find BOM ');
}
}else{
Return (' No BOM found ');
}
}
function rewrite ($filename, $data) {
$filenum =fopen ($filename, ' w ');
Flock ($filenum, LOCK_EX);
Fwrite ($filenum, $data);
Fclose ($filenum);
}
Save the above code as a file with the suffix PHP in the file directory where the BOM needs to be removed, and then run the PHP file, the directory and all subdirectories of the directory will be checked for BOM and the BOM is removed, the running results page is as follows: