I have script that reads remote file content and writes it to local server. File contains characters: ąčęėįšųūž. After data insertion into local file, UTF-8 encoding is lost. My script code:
<?php
$data = file_get_contents('remote_file_address');
echo $data; //encoding is ok
$file = dirname(__FILE__) . '/../downloads/data.csv';
file_put_contents($file,$data); //invalid encoding in data.csv file
?>
I also followed the instructions depending this post(How to write file in UTF-8 format?), but still no good.
So what is wrong with that? Any ideas?
The utf8_encode() function is an inbuilt function in PHP which is used to encode an ISO-8859-1 string to UTF-8. Unicode has been developed to describe all possible characters of all languages and includes a lot of symbols with one unique number for each symbol/character.
The file_put_contents() function in PHP is an inbuilt function which is used to write a string to a file. The file_put_contents() function checks for the file in which the user wants to write and if the file doesn't exist, it creates a new file.
Definition and Usage. The utf8_encode() function encodes an ISO-8859-1 string to UTF-8. Unicode is a universal standard, and has been developed to describe all possible characters of all languages plus a lot of symbols with one unique number for each character/symbol.
The problem was remote file with windows-1257 encoding. I found the solution here.
So the correct code should look like this:
<?php
$data = file_get_contents('remote_file_address');
$data = iconv("CP1257","UTF-8", $data);
$file = dirname(__FILE__) . '/../downloads/data.csv';
file_put_contents($file,$data);
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With