PHP instance: detects and clears the BOM information starting with the file.
- Ini_set ('memory _ limit ','-1 ');
- /* Check and clear BOM */
- $ Basedir = dirname (_ FILE _); // scan the current FILE path and set it automatically
- $ 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 );
- }
- }
- } // End while
- Closedir ($ dh );
- } // End if ($ dh
- } // End function
- Function checkBOM ($ filename ){
- Global $ auto; // bbs.it-home.org
- $ 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 "BOM found, automatically removed .";
- } Else {
- Return ("BOM found .");
- }
- }
- Else return ("BOM Not Found .");
- } // End function
- Function rewrite ($ filename, $ data ){
- $ Filenum = fopen ($ filename, "w ");
- Flock ($ filenum, LOCK_EX );
- Fwrite ($ filenum, $ data );
- Fclose ($ filenum );
- } // End function
|