PHP BOM Removal tool, PHP batch removal of BOM code

Source: Internet
Author: User
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:

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.