Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: kodowanie UTF-8 w pliku .txt za pomocą fwrite()
Forum PHP.pl > Forum > PHP
kokogdaczek
witam!

Potrzebuję jakiegoś kodu w php za pomocą którego funkcja fwrite() zapisze w pliku .txt ciąg znaków w kodowaniu UTF-8.
pojas
Może trzeba wykorzystać funkcję utf8_encode" title="Zobacz w manualu PHP" target="_manual, żeby zakodować treść, którą następnie zapiszesz do pliku. guitar.gif


Cytat(cutmaster at fearlesss dot com)
For those who, like me, lost a lot of minutes (hours) to understand why fwrite doesn't create a real utf-8 file, here's the explanation I've found :

I tried to do something like this :
  1. <?php 
  2. $myString = utf8_encode("Test with accents éčŕç"); 
  3. $fh=fopen('test.xml',"w"); 
  4. fwrite($fh,$myString); 
  5. fclose($fh); 
  6. ?>


For a mysterious reason, the resulted file shows the accent without the utf-8 conversion.

I tried the binary, mode, etc. etc. And finally I've found it :
It seems that fwrite NEEDS to have the utf8_encode function INSIDE its parameters like this, to understand it must create a non-text only file :
  1. <?php 
  2. $myString = "Test with accents éčŕç"; 
  3. $fh=fopen('test.xml',"w"); 
  4. fwrite($fh,utf8_encode($myString)); 
  5. fclose($fh); 
  6. ?>

Hope this will help
kokogdaczek
Wielkie dzięki, o to mi chodziło biggrin.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.