Can't remove \ufeff from a string

Viewed 11784

The app basically works like this:

1) The user uploads a CSV file.

2) The file is catched by PHP via POST.

3) I open the file with fopen() and read the file with fgetcsv().

The first column it always have the \ufeff char. I know that is called UTF-8 BOM, and it's generated by Microsoft Excel. But, when I want to remove that, I can't.

I've tried: str_replace('\ufeff', '', $columns[0]);

4 Answers
$columns[0] = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $columns[0]);

The above code helps you remove hidden characters that exist in your document, just like the one you mentioned.

$result = trim($result, "\xEF\xBB\xBF");

This is the simplest way to solve it.

$headings=array();    
$handle = fopen($_FILES["contacts_file"]["tmp_name"], "r");  
$heading_data=fgetcsv($handle);
             foreach($heading_data as $heading){

              // Remove any invalid or hidden characters
              $heading = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $heading);

              array_push($headings, $heading);
         }

$columns[0] = preg_replace('/\xEF\xBB\xBF/', '', $columns[0]);

or

$columns[0] = trim($columns[0], "\xEF\xBB\xBF");

Related