note 113023 added to function.fgetcsv
| From: | timkofoed83 at gmail dot com | Date: | Mon, 19 Aug 2013 18:59:01 +0000 |
| Subject: | note 113023 added to function.fgetcsv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-196022@lists.php.net to get a copy of this message | ||
Situation: You need to read a CSV file in a non-UTF-8 charset, convert to UTF-8 and compensate for
special characters, then return an encoded JSON response.
(json_encode can only use UTF-8 encoding)
In my case, the PHP functions (utf8_encode and mb_convert_encoding) did not work, however it can be
made to work by going through HTML notation (using PHP 5.2.6-1).
In this example, I'm converting special characters from the CSV file with the charset "Mac
OS Roman" to UTF-8 to be sent to an iOS application as a JSON response:
if (($handle = fopen($csvDestination, "r")) !== FALSE)
{
$lineNumber = 0;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$rowArray = array();
//Convert the line from "Mac OS Roman" to HTML notation
$dataElement0 = htmlentities($data[0], ENT_QUOTES | ENT_SUBSTITUTE, "MacRoman");
$dataElement1 = htmlentities($data[1], ENT_QUOTES | ENT_SUBSTITUTE, "MacRoman");
$dataElement2 = htmlentities($data[2], ENT_QUOTES | ENT_SUBSTITUTE, "MacRoman");
$dataElement3 = htmlentities($data[3], ENT_QUOTES | ENT_SUBSTITUTE, "MacRoman");
$dataElement4 = htmlentities($data[4], ENT_QUOTES | ENT_SUBSTITUTE, "MacRoman");
$dataElement5 = htmlentities($data[5], ENT_QUOTES | ENT_SUBSTITUTE, "MacRoman");
$dataElement6 = htmlentities($data[6], ENT_QUOTES | ENT_SUBSTITUTE, "MacRoman");
$dataElement7 = htmlentities($data[7], ENT_QUOTES | ENT_SUBSTITUTE, "MacRoman");
$dataElement8 = htmlentities($data[8], ENT_QUOTES | ENT_SUBSTITUTE, "MacRoman");
//convert HTML notation to UTF-8
$dataElement0 = html_entity_decode($dataElement0, ENT_QUOTES, 'UTF-8');
$dataElement1 = html_entity_decode($dataElement1, ENT_QUOTES, 'UTF-8');
$dataElement2 = html_entity_decode($dataElement2, ENT_QUOTES, 'UTF-8');
$dataElement3 = html_entity_decode($dataElement3, ENT_QUOTES, 'UTF-8');
$dataElement4 = html_entity_decode($dataElement4, ENT_QUOTES, 'UTF-8');
$dataElement5 = html_entity_decode($dataElement5, ENT_QUOTES, 'UTF-8');
$dataElement6 = html_entity_decode($dataElement6, ENT_QUOTES, 'UTF-8');
$dataElement7 = html_entity_decode($dataElement7, ENT_QUOTES, 'UTF-8');
$dataElement8 = html_entity_decode($dataElement8, ENT_QUOTES, 'UTF-8');
//make the JSON response
$rowArray['id'] = strval(++$lineNumber);
$rowArray['country'] = $dataElement0;
$rowArray['name'] = $dataElement1;
$rowArray['address-1'] = $dataElement2;
$rowArray['address-2'] = $dataElement3;
$rowArray['latitude'] = $dataElement4;
$rowArray['longitude'] = $dataElement5;
$rowArray['telephone'] = $dataElement6;
$rowArray['region'] = $dataElement7;
$rowArray['continent'] = $dataElement8;
array_push($responseArray, $rowArray);
}
fclose($handle);
echo json_encode($responseArray);
}
NOTE: This example can't read danish characters (æ ø å). fgetcsv will ignore Danish
characters. To circumvent this, instead of using fopen(), use:
$lines = file($csvDestination);
foreach ($lines as $line_num => $line)
{
$convertedLine = htmlentities($line, ENT_QUOTES | ENT_IGNORE | ENT_SUBSTITUTE,
"MacRoman");
$convertedLine = html_entity_decode($convertedLine, ENT_QUOTES, 'UTF-8');
//now you have the correct string, and can either use str_getcsv, or in my case using PHP 5.2.6, I
had to parse the line manually.
}
----
Server IP: 195.184.96.15
Probable Submitter: 109.70.54.100
----
Manual Page -- http://php.net/manual/en/function.fgetcsv.php
Edit -- https://master.php.net/note/edit/113023
Del: integrated -- https://master.php.net/note/delete/113023/integrated
Del: useless -- https://master.php.net/note/delete/113023/useless
Del: bad code -- https://master.php.net/note/delete/113023/bad+code
Del: spam -- https://master.php.net/note/delete/113023/spam
Del: non-english -- https://master.php.net/note/delete/113023/non-english
Del: in docs -- https://master.php.net/note/delete/113023/in+docs
Del: other reasons-- https://master.php.net/note/delete/113023
Reject -- https://master.php.net/note/reject/113023
Search -- https://master.php.net/manage/user-notes.php