PHP batch remove BOM tool source code

Source: Internet
Author: User

Bom is a headache. Many problems are caused by garbled code generated during php development. This tool has been found on the Internet and has been optimized by myself, making it easier to use. if you need it, please feel free to use it without any side effects.

CodeAs follows:

<? Phpheader ("Content-Type: text/html; charset = UTF-8"); If (isset ($ _ Get ['dir']) {// set the file directory $ 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 "File Name: $ basedir/$ File ". checkbom ("$ basedir/$ File "). "<br>";} 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 ("<font color = Red> BOM found and deleted automatically </font> ");} else {return ("<font color = Red> BOM found </font>") ;}} else return ("Bom not found");} function rewrite ($ filename, $ data) {$ filenum = fopen ($ filename, "W"); flock ($ filenum, lock_ex); fwrite ($ filenum, $ data ); fclose ($ filenum) ;}?>

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.