note 104325 modified in function.fopen by danbrown

From: Date: Thu, 09 Jun 2011 13:40:32 +0000
Subject: note 104325 modified in function.fopen by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-180383@lists.php.net to get a copy of this message
While opening a file with multibyte data (Ex: données multi-octets), faced some issues with the encoding. Got to know that it uses windows-1250. Used iconv to convert it to UTF-8 and it resolved the issue. <?php function utf8_fopen_read($fileName) { $fc = iconv('windows-1250', 'utf-8', file_get_contents($fileName)); $handle=fopen("php://memory", "rw"); fwrite($handle, $fc); fseek($handle, 0); return $handle; } ?> Example usage: <?php $fh = utf8_fopen_read("./tpKpiBundle.csv"); while (($data = fgetcsv($fh, 1000, ",")) !== false) { foreach ($data as $value) { echo $value . "<br />\n"; } } ?> Hope it helps. --was-- While opening a file with multibyte data (Ex: données multi-octets), faced some issues with the encoding. Got to know that it uses windows-1250. Used iconv to convert it to UTF-8 and it resolved the issue. function utf8_fopen_read($fileName) { $fc = iconv('windows-1250', 'utf-8', file_get_contents($fileName)); $handle=fopen("php://memory", "rw"); fwrite($handle, $fc); fseek($handle, 0); return $handle; } Example usage: $fh = utf8_fopen_read("./tpKpiBundle.csv"); while (($data = fgetcsv($fh, 1000, ",")) !== false) { foreach ($data as $value) { echo $value . "<br />\n"; } } Hope it helps. http://php.net/manual/en/function.fopen.php

« previous php.notes (#180383) next »