Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fwrite() and UTF8

Tags:

php

utf-8

fwrite

I am creating a file using php fwrite() and I know all my data is in UTF8 ( I have done extensive testing on this - when saving data to db and outputting on normal webpage all work fine and report as utf8.), but I am being told the file I am outputting contains non utf8 data :( Is there a command in bash (CentOS) to check the format of a file?

When using vim it shows the content as:

Donâ~@~Yt do anything .... Itâ~@~Ys a great site with everything....Weâ~@~Yve only just launched/

Any help would be appreciated: Either confirming the file is UTF8 or how to write utf8 content to a file.

UPDATE

To clarify how I know I have data in UTF8 i have done the following:

  1. DB is set to utf8 When saving data
  2. to database I run this first:

    $enc = mb_detect_encoding($data);

    $data = mb_convert_encoding($data, "UTF-8", $enc);

  3. Just before I run fwrite i have checked the data with Note each piece of data returns 'IS utf-8'

    if (strlen($data)==mb_strlen($data, 'UTF-8')) print 'NOT UTF-8'; else print 'IS utf-8';

Thanks!

like image 805
Lizard Avatar asked Jun 13 '11 21:06

Lizard


People also ask

What is SIG utf8?

"sig" in "utf-8-sig" is the abbreviation of "signature" (i.e. signature utf-8 file). Using utf-8-sig to read a file will treat the BOM as metadata that explains how to interpret the file, instead of as part of the file contents.

How do I save a UTF-8 file in python?

open() to read from a file and save its contents to a UTF-8 file. Use io. open(filename, mode, encoding="utf8") with mode as "r" to open filename in reading mode with its text encoded to UTF-8.

What is Fwrite PHP?

PHP - Function fwrite()The fwrite() function can write to an open file. The function can stop at the end of a file or when it can reach a specified length, whichever comes first. This function can return the number of bytes written or false on failure.


1 Answers

//add BOM to fix UTF-8 in Excel
fputs($fp, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));

I find this piece works for me :)

like image 175
Du Peng Avatar answered Sep 23 '22 19:09

Du Peng