Problem writing UTF-8 encoded file in PHP

I have a large file that contains countries of the world / regions, which I divide into smaller files based on individual countries / regions. The source file contains entries like:

  EE.04 Järvamaa
  EE.05 Jõgevamaa
  EE.07 Läänemaa

However, when I extract this and write it to a new file, the text becomes:

  EE.04  Järvamaa
  EE.05  Jõgevamaa
  EE.07  Läänemaa

To save my files, I use the following code:

mb_detect_encoding($text, "UTF-8") == "UTF-8" ? : $text = utf8_encode($text);
$fp = fopen(MY_LOCATION,'wb');
fwrite($fp,$text);
fclose($fp);

I tried to save files with and without utf8_encode (), and none of them work. How can I save the original encoding (which is UTF8)?

Thank!

+3
source share
5 answers

, mb_detect_encoding. , , ( , ).

mb_detect_encoding.

, utf8_encode Latin-1 UTF-8 ( UTF-8, )... iconv, ( mb_detect_encoding, - ).

iconv $str = iconv('', 'UTF-8', $str); ( )...

+4

. utf8_encode ($ theString), UTF8.

UTF-8.

, : - http://en.wikipedia.org/wiki/Byte_order_mark
- http://unicode.org/faq/utf_bom.html

: UTF-8 '\ xef\xbb\xbf', .

<?php
function writeStringToFile($file, $string){
$f=fopen($file, "wb");
$file="\xEF\xBB\xBF".$string; // utf8 bom
fputs($f, $string);
fclose($f);
}
?>

$file xml... $- UTF8.

, UTF8 () UTF8.

writeStringToFile('test.xml', 'éèàç');
+1

, htmlentities($text) , html_entity_decode($fetchedData) . .

0

:

<?php
$s = "This is a string éèàç and it is in utf-8";
$f = fopen('myFile',"w");
fwrite($f, utf8_encode($s));
fclose($f);
?> 
-1

It looks like your source file is not, in fact, in UTF-8. You might want to use the same approach that you used, but with a different encoding, for example, UTF-16.

-1
source

Source: https://habr.com/ru/post/1760775/


All Articles