PHP fix and space not working

I have data imported from csv. The import script captures all email addresses in csv and, after checking them, imports them in db.

The client provided this csv, and some of the emails seem to take place at the end of the cell. No problem, cut that sucker ... no, it doesn't work.

The space does not seem to be space and is not deleted, so email validation fails.

Question: In any case, I can determine what this erroneous character is, and how to remove it?

Not sure if its some funky encoding or something else going on, but I'm not going to go through and delete them all manually! If I UTF-8 encodes the string first, it shows this character as:

A

+7
php encoding trim space
source share
4 answers

If trim() not affected by this "space", the first step is to identify it.

Use urlencode() in a string. Urlencode will be a percentage - to avoid any non-printable and many printable characters except ASCII, so you will immediately see the hex code of the characters who violated. Depending on what you find, you can act accordingly or update your question to get more help.

+18
source share

I had a similar problem, as well as downloading emails from CSV and problems with "undetectable" spaces.

Allowed it, replacing the most common urlencoded whitespace characters with ''. This can help if you cannot use mb_detect_encoding () and / or iconv ()

  $urlEncodedWhiteSpaceChars = '%81,%7F,%C5%8D,%8D,%8F,%C2%90,%C2,%90,%9D,%C2%A0,%A0,%C2%AD,%AD,%08,%09,%0A,%0D'; $temp = explode(',', $urlEncodedWhiteSpaceChars); // turn them into a temp array so we can loop accross $email_address = urlencode($row['EMAIL_ADDRESS']); foreach($temp as $v){ $email_address = str_replace($v, '', $email_address); // replace the current char with nuffink } $email_address = urldecode($email_address); // undo the url_encode 

Note that this does NOT discard the β€œnormal” space character and that it removes these space characters from anywhere in the line β€” not just the beginning or end.

+2
source share

I see a couple of possible solutions

1) Get the last char string in PHP and check if it is a normal character (e.g. with a regular expression). If this is not an ordinary character, delete it.

 $length = strlen($string); $string[($length-1)] = ''; 

2) Convert your character from UTF-8 to the encoding of the CSV file and use str_replace. For example, if CSV is encoded in ISO-8859-2

 echo iconv('UTF-8', 'ISO-8859-2', "Γ‚"); 
0
source share

In most cases, simple strip_tags($string) will work.

If the above does not work, you should try to identify the characters resorting to urlencode() , and then act accordingly.

0
source share

All Articles