如何在PHP中将ASCII编码的字符串转换为UTF8? [重复]

This question already has an answer here:

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

</div>

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.

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

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

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?