PHP deletes the utf-8BOM instance code. In an earlier PHP Tutorial, we mentioned an example of using PHP to determine whether a file is UTF-8 encoded (and check Bom). today let's implement PHP to delete the utf-8BOM, in the earlier PHP Tutorial, we mentioned the example of using PHP to determine whether the file is UTF-8 code (and check the Bom). today we will implement PHP to delete the UTF-8 BOM, let's talk less about the code directly:
Copy to ClipboardReference: [www.bkjia.com] // Find out the UTF-8 BOM problem
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 ("exist, automatically delete .");
} Else {
Return ("nonexistent ");
}
}
Else return ("nonexistent ");
}
Function rewrite ($ filename, $ data ){
$ Filenum = fopen ($ filename, "w ");
Flock ($ filenum, LOCK_EX );
Fwrite ($ filenum, $ data );
Fclose ($ filenum );
}
?>
Example of deleting (and check Bom). today we will try to delete the UTF-8 BOM in PHP...