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:
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