In this paper, we bring a Unicode signature (BOM) method that uses PHP to remove the file header. Small series feel very good, now share to everyone, also for everyone to make a reference. Follow the small series together to see it, I hope you can help everyone.
Nonsense not much to say directly on the code
<?php//this file is used to quickly test UTF8 encoded files is not added BOM, and can automatically remove//by Bob shen$basedir= "."; Modify this behavior needs to detect the directory, point represents the current directory $auto=1; Whether the discovered BOM information is automatically removed. 1 for Yes, 0 for No. The following does not change if ($dh = Opendir ($basedir)) {while (($file = Readdir ($DH))!== false) {if ($file! = '. ' && $file! = ') '. ' & ;&!is_dir ($basedir. " /". $file)" echo "FileName: $file". Checkbom ("$basedir/$file"). "<br>";} Closedir ($DH);} function Checkbom ($filename) {$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) {$rest =substr ($contents, 3); rewrite ($filename, $rest); return true;} else return ("BOM not Found.");} function rewrite ($filename, $data) {$filenum =fopen ($filename, "w"), Flock ($filenum, LOCK_EX); Fwrite ($filenum, $data); Fclose ($filenum);}?