Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP file_put_contents and UTF-8 [closed]

Tags:

php

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?

like image 973
Bounce Avatar asked Jun 20 '12 08:06

Bounce


People also ask

Does PHP support UTF-8?

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.

What is File_put_contents?

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.

What is UTF-8 PHP?

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.


1 Answers

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);

?>
like image 125
Bounce Avatar answered Sep 28 '22 10:09

Bounce