Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove multiple UTF-8 BOM sequences

Using PHP5 (cgi) to output template files from the filesystem and having issues spitting out raw HTML.

private function fetch($name) {     $path = $this->j->config['template_path'] . $name . '.html';     if (!file_exists($path)) {         dbgerror('Could not find the template "' . $name . '" in ' . $path);     }     $f = fopen($path, 'r');     $t = fread($f, filesize($path));     fclose($f);     if (substr($t, 0, 3) == b'\xef\xbb\xbf') {         $t = substr($t, 3);     }     return $t; } 

Even though I've added the BOM fix I'm still having problems with Firefox accepting it. You can see a live copy here: http://ircb.in/jisti/ (and the template file I threw at http://ircb.in/jisti/home.html if you want to check it out)

Any idea how to fix this? o_o

like image 799
sheppardzw Avatar asked Apr 24 '12 02:04

sheppardzw


People also ask

How do I get rid of BOM?

How to remove BOM. If you want to remove the byte order mark from a source code, you need a text editor that offers the option of saving the mark. You read the file with the BOM into the software, then save it again without the BOM and thereby convert the coding. The mark should then no longer appear.

What is the difference between UTF-8 and UTF-8 BOM?

The UTF-8 BOM is a sequence of bytes at the start of a text stream ( 0xEF, 0xBB, 0xBF ) that allows the reader to more reliably guess a file as being encoded in UTF-8. Normally, the BOM is used to signal the endianness of an encoding, but since endianness is irrelevant to UTF-8, the BOM is unnecessary.

Should you use UTF-8 with BOM?

The Unicode Standard permits the BOM in UTF-8, but does not require or recommend its use. Byte order has no meaning in UTF-8, so its only use in UTF-8 is to signal at the start that the text stream is encoded in UTF-8, or that it was converted to UTF-8 from a stream that contained an optional BOM.


2 Answers

you would use the following code to remove utf8 bom

//Remove UTF8 Bom  function remove_utf8_bom($text) {     $bom = pack('H*','EFBBBF');     $text = preg_replace("/^$bom/", '', $text);     return $text; } 
like image 197
jasonhao Avatar answered Sep 28 '22 22:09

jasonhao


try:

// -------- read the file-content ---- $str = file_get_contents($source_file);   // -------- remove the utf-8 BOM ---- $str = str_replace("\xEF\xBB\xBF",'',$str);   // -------- get the Object from JSON ----  $obj = json_decode($str);  

:)

like image 23
o1max Avatar answered Sep 28 '22 22:09

o1max