Username: 
Password: 
Restrict session to IP 

PHP and remove UTF8 Byte Order Mark

Global Rank: 252
Totalscore: 87267
Posts: 1635
Thanks: 1337
UpVotes: 886
Registered: 16y 43d




Last Seen: 19h 30s
The User is Offline
PHP and remove UTF8 Byte Order Mark
Google/translate1Thank You!0Good Post!1Bad Post! link
Hello,

I just wanted to share a snippet of source, to remove the ByteOrderMark in utf8 files.
Maybe some of you encountered weird output when including utf8 files in php. I have seen that on one or two participating sites.

If you get output like "", it`s the UTF8-ByteOrderMark (BOM).

The BOM is totally useless in UTF8, but php does not ignore it. Instead PHP will print the BOM, causing any http-header calls to be useless.

Here is a script that will remove the BOM from all files recursively from the location the code is called:

GeSHi`ed PHP code
1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
43
44
4546
47
48
49
5051
52
53
54
5556
57
58
59
6061
62
63
64
6566
67
68
69
7071
72
 
<?php
bom_rec('.'); // Do it!
 
/** * Recursively check dir
 * @param unknown_type $path
 * @return unknown_type
 */
function bom_rec($path){
        if (false === ($dir = dir($path))) {
                return false;
        }
                while (false !== ($entry = $dir->read()))
        {
                if ($entry === '.' || $entry === '..') {
                        continue;
                }                
                $fullpath = $path.'/'.$entry;
                if (is_dir($fullpath)) {
                        bom_rec($fullpath);
                } else {                        bom_fix($fullpath);     
                }
        }
        
        $dir->close();}
 
 
/**
 * Check for magic bytes. * @param $filename
 * @return unknown_type
 */
function bom_need_fix($filename)
{        if (false === ($fh = fopen($filename, 'rb'))) {
                return false;
        }
 
        $fix_me = false;        
        $one = ord(fgetc($fh));
        $two = ord(fgetc($fh));
        $three = ord(fgetc($fh));
        if ($one === 239 && $two === 187 && $three == 191) {                $fix_me = true;
        }
        
        fclose($fh);
                return $fix_me;
}
 
/**
 * Remove magic bytes. * @param $filename
 * @return unknown_type
 */
function bom_fix($filename)
{        if (bom_need_fix($filename)) {
                var_dump('FIXING '.$filename);
                file_put_contents($filename, substr(file_get_contents($filename), 3));
        }
}?>
 


Have fun Smile
The geeks shall inherit the properties and methods of object earth.
tunelko, quangntenemy, TheHiveMind, Z, balicocat, Ge0, samuraiblanco, arraez, jcquinterov, hophuocthinh, alfamen2, burhanudinn123, Ben_Dover, stephanduran89, braddie0, SwolloW, dangarbri have subscribed to this thread and receive emails on new posts.
1 people are watching the thread at the moment.
This thread has been viewed 3453 times.