PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
It is currently Mon Feb 18, 2019 3:24 am

All times are UTC - 5 hours

Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Sat May 13, 2006 3:40 pm 
Forum Regular
User avatar

Joined: Tue Sep 23, 2003 11:07 pm
Posts: 769
Location: The Republic of Texas
Y0, time for another phat "why the hell would I ever need that" release by yours truly!

This is a simple, and I do mean simple, php script to remove UTF-8 BOMs from files in the cwd.

Since most web browsers, text editors don't show the BOM, I recommend you copy and paste this useless code into a terminal type editor like ... "pico" (you know vi sucks).

Syntax: [ Download ] [ Hide ]
#!/usr/bin/php -q



   Description: Script to Remove Byte Order Marks (BOM) from UTF-8 Files in Current Working Directory

   Q. What Is A BOM?


   Usage: 1. Copy this script into a directory with files to check for BOM's.

          2. Make it executable (chmod +x defusebom.php)

          3. Execute (./defusebom.php)


$cwd        = dirname(".");

$dh         = opendir($cwd);

$haystack   = "";    // Initialized

$needle     = ""; // DO NOT EDIT

$reportonly = FALSE; // Set to TRUE to Only Report Which Files Have a BOM

while (false !== ($file = readdir($dh))) {

    if (!is_dir("$cwd/$file") && $file != basename($_SERVER["PHP_SELF"])) {

        $haystack = file_get_contents("$cwd/$file");

        if (substr_count($haystack, $needle)) {

            if (!$reportonly) {

                // Uh oh, a BOM was found, defuse it!

                $newfile = str_replace($needle, "", $haystack);

                if (is_writable("$cwd/$file")) {

                    // Open File For Writing

                    $handle = fopen("$cwd/$file", "w");

                    fwrite($handle, $newfile);


                    echo "Defused BOM In: ".$file."\n";

                } else {

                    echo "File: ".$file." had a BOM that couldn't be defused.\n";


            } else {

                echo "File: ".$file." Has a BOM!\n";






Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 5 hours

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group