-1

I am reading CSV file to PHP. File is reading properly except one thing. Issue is in reading words which are in non-English language.

Array is returning some symbols instead of other language exact word.

For eg: LBL_Hello Hello 你好

Above contents are there in CSV file. I am reading the words into PHP in form of Array using below code:

<?php
$file = file_get_contents("test.csv");
    $data = (array_map("str_getcsv", preg_split('/\r*\n+|\r+/', $file)));
    echo "<pre>";
    print_r(($data));
    exit;

?>

Output:

Array
(
    [0] => Array
        (
            [0] => LBL_Hello
            [1] => Hello
            [2] => ���
        )

    [1] => Array
        (
            [0] => 
        )

)

Here, Chinese word "你好" is showing as "���". This is happened with all other language words also.

Please help me to know what is missing so that I can get the exact other language word while reading CSV into PHP.

Thanks in Advance.

1 Answers1

-1

Try sending charset headers at the top of file(before any other output)

header('Content-Type: text/html; charset=UTF-8')

Sample Code to get data(contains Non-English characters) from CSV:

<?php
header('Content-Type: text/html; charset=UTF-8');
$handle = fopen ("test.csv", "r");
$dataArr = [];
echo "<pre>";
while ($data = fgetcsv ($handle, 1000, ",")) {
    array_push($dataArr, $data);
}
print_r($dataArr);
echo "</pre>";
exit;
?>
Sam G
  • 1,242
  • 15
  • 12