Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange characters in first row of array after fgetcsv

I have a CSV file where the first "cell" is just an int, 9, in this case. The next line is 10 for the first "cell" and so on. When I do $array = fgetcsv($file); the first cell of the first line has these weird characters in front of the value: ˇ˛

It's messing with my database import since this cell is supposed to only contain an int. It only happens on the first cell of the first line.

Any ideas on why this is happening and what I can do to avoid it?

like image 643
CR47 Avatar asked Nov 21 '13 15:11

CR47


3 Answers

As others suggested, weird characters are Byte Order Mark (BOM). In order to remove it you can use following snippet:

if (mb_detect_encoding($value) === 'UTF-8') {
    // delete possible BOM
    // not all UTF-8 files start with these three bytes
    $value = preg_replace('/\x{EF}\x{BB}\x{BF}/', '', $value);
}
like image 68
hpaknia Avatar answered Nov 14 '22 16:11

hpaknia


I ran into this problem today. I had these results appear for the first result of the first row:

123465

The solution I had was to add this to my HTML head:

<meta charset="UTF-8">

The result then became:

123456

This is because my CSV file was encoded in UTF-8, so by declaring the character set as UTF-8 I was able to get the intended results.

like image 20
Muhammad Abdul-Rahim Avatar answered Nov 14 '22 17:11

Muhammad Abdul-Rahim


Sounds like you have a unicode file and are picking up the Byte Order Mark.

like image 1
elixenide Avatar answered Nov 14 '22 17:11

elixenide