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?
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);
}
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.
Sounds like you have a unicode file and are picking up the Byte Order Mark.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With