note 106463 deleted from function.fopen by tularis
| From: | tularis@php.net | Date: | Fri, 11 Nov 2011 16:32:13 +0000 |
| Subject: | note 106463 deleted from function.fopen by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-184392@lists.php.net to get a copy of this message | ||
Note Submitter: lukas_panek at yahoo dot com
----
I just spent a couple of hours (again) trying to figure out character encoding issues in my script
and I thought it might help someone else too.
<?php
/* this file contains Czech language characters,
* such as ÄÅ¡ÄÅžýáÃéúůÄÅ¥Å
*/
$f=fopen('czech_text.csv','rb');
$cze=array();
$i=0;
while(!feof($f)){
$row=fgets($f,4096);
$parts=explode(';',strtolower(rtrim($row)));
$cz[$i]=array();
$cz[$i]['cz']=$parts[0];
$cz[$i++]['lat']=$parts[1];
}
fclose($f);
/* this will probably not output any Czech chars */
var_dump('<pre>',$cz,'<pre>');
?>
What I did was I changed
<?php
$parts=explode(';',strtolower(rtrim($row)));
?>
to
<?php
$parts=explode(';',mb_strtolower(rtrim($row)));
?>
and added
<?php
mb_internal_encoding('UTF-8');
?>
to the very beginning, which was the most crucial thing.
Also note that the use of the mb_split(); function changes nothing on the resulting array.
Fiddling around with character encodings can and IS really frustrating, so good luck everyone out
there doing the same.