note 106463 added to function.fopen
| From: | lukas_panek at yahoo dot com | Date: | Thu, 10 Nov 2011 23:21:02 +0000 |
| Subject: | note 106463 added to function.fopen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-184382@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 69.147.83.197
Probable Submitter: 90.180.86.214
----
Manual Page -- http://www.php.net/manual/en/function.fopen.php
Edit -- https://master.php.net/note/edit/106463
Del: integrated -- https://master.php.net/note/delete/106463/integrated
Del: useless -- https://master.php.net/note/delete/106463/useless
Del: bad code -- https://master.php.net/note/delete/106463/bad+code
Del: spam -- https://master.php.net/note/delete/106463/spam
Del: non-english -- https://master.php.net/note/delete/106463/non-english
Del: in docs -- https://master.php.net/note/delete/106463/in+docs
Del: other reasons-- https://master.php.net/note/delete/106463
Reject -- https://master.php.net/note/reject/106463
Search -- https://master.php.net/manage/user-notes.php