0

I tried to do: file_put_contents ( $file_name, utf8_encode($data) ) ; But when i check the file encoding from the shell with the linux command: 'file file_name' I get: 'file_name: ASCII text'

Does it mean that the utf8_encoding didn't worked? if so, what is the right way to convert from ASCII to UTF8

4 Answers4

1

If your string doesn't contain any non-ASCII characters, then you likely won't see differences, since UTF-8 is backwards compatible with ASCII. Try writing, for example, the text "1000 さくら" and see what happens.

rid
  • 61,078
  • 31
  • 152
  • 193
1

Please note that utf8_encode only converts a string encoded in ISO-8859-1 to UTF-8. A more appropriate name for it would be "iso88591_to_utf8". If your text is not encoded in ISO-8859-1, you do not need this function. If your text is already in UTF-8, you do not need this function. In fact, applying this function to text that is not encoded in ISO-8859-1 will most likely simply garble that text.

If you need to convert text from any encoding to any other encoding, look at iconv() instead.

See http://php.net/manual/en/function.utf8-encode.php

1

ASCII is a subset of UTF-8, so if a document is ASCII then it is already UTF-8

Found at: Convert ASCII TO UTF-8 Encoding

Community
  • 1
  • 1
Tim
  • 662
  • 1
  • 7
  • 16
0

Try this:

$data = mb_convert_encoding($data, 'UTF-8', 'ASCII');
file_put_contents ( $file_name, $data );

or use this to change file encoding:

$fd = fopen($file, 'r');
stream_filter_append($fd, 'convert.iconv.UTF-8/ASCII');
stream_copy_to_stream($fd, fopen($output, 'w'));

Reference: How to write file in UTF-8 format?

Community
  • 1
  • 1
Mojtaba Rezaeian
  • 8,268
  • 8
  • 31
  • 54