Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't remove \ufeff from a string

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]);

like image 492
Angel Luis Avatar asked Jan 11 '19 10:01

Angel Luis


People also ask

How do I get rid of uFEFF in Python?

when you view the code of file using read() function you can see at the begin of the returned code '\ufeff' is shown. The one simplest solution to this problem is just by changing the encoding back to ASCII encoding(for this you can copy your code to a notepad and save it Remember!

What is uFEFF in Python?

The Unicode character U+FEFF is the byte order mark, or BOM, and is used to tell the difference between big- and little-endian UTF-16 encoding. If you decode the web page using the right codec, Python will remove it for you.

What is uFEFF in Javascript?

Yeah, UFEFF is the UTF8 byte order mark, which a lot of tools have trouble parsing. I'd just use standard UTF8 encoding without it for compatibility reasons. All reactions. Member.


3 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.

like image 104
pr1nc3 Avatar answered Oct 05 '22 22:10

pr1nc3


$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);
         }
like image 28
james ace Avatar answered Oct 06 '22 00:10

james ace


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

This is the simplest way to solve it.

like image 37
Terry Lin Avatar answered Oct 06 '22 00:10

Terry Lin