In the previous PHP tutorial, we talked about using PHP to determine whether the file is UTF-8 encoding (and check the BOM) example, today we will implement the PHP delete utf-8 BOM, crap less directly on the code:
Copy to ClipboardWhat to refer to:
[www.bkjia.com] Find Utf-8 BOM Issues
if (Isset ($_get[' dir ')) {
$basedir =$_get[' dir '];
}else{
$basedir = '. ';
}
$auto = 1;
Checkdir ($basedir);
function Checkdir ($basedir) {
if ($dh = Opendir ($basedir)) {
while (($file = Readdir ($DH))!== false) {
if ($file! = '. ' && $file! = ' ... ') {
if (!is_dir ($basedir. " /". $file)) {
echo "FileName: $basedir/$file". Checkbom ("$basedir/$file"). "
";
}else{
$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 ("Existing, automatic deletion.");
} else {
Return ("does not exist");
}
}
else return ("does not exist");
}
function rewrite ($filename, $data) {
$filenum = fopen ($filename, "w");
Flock ($filenum, LOCK_EX);
Fwrite ($filenum, $data);
Fclose ($filenum);
}
?>
http://www.bkjia.com/PHPjc/364742.html www.bkjia.com true http://www.bkjia.com/PHPjc/364742.html techarticle earlier PHP tutorial, we mentioned about using PHP to determine whether the file is UTF-8 encoding (and check the BOM) example, today we will implement the PHP delete utf-8 BOM, crap less directly on ...