guillefix
guillefix

Reputation: 994

Getting unknown characters between each letter when reading uploaded CSV on Laravel

I'm trying to upload a CSV file, read its contents and import that data into a database, but apparently I'm getting some kind of bug where an unknown character shows between each character.

This is my code:

    $file = $request->file('file');
    $fileName = $file->getRealPath();
    $file = fopen($fileName, "r");

    $i = 0;
    while (($column = fgetcsv($file, 10000, ";")) !== FALSE) {

        if ($i == 0){
            $i++;
            continue;
        }
        echo print_r($column);$i++;    
    }

    fclose($file);

This is what I get from print_r:

enter image description here

Any ideas on what could it be? I've tried opening the CSV with encoding UTF8 and UTF16 but I still have this issue.

Thanks in advance.

Upvotes: 1

Views: 373

Answers (1)

Justinas
Justinas

Reputation: 43507

Most likely it's \0 char that some system appends. Check with ord($string[0])

You can simply do str_replace("\0", '', $string)

Or try some regex replace: preg_replace('/[^\w\d-.,\s]/', '', $string)

Upvotes: 1

Related Questions