PHP trim and space not working PHP trim and space not working php php

PHP trim and space not working


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

Use urlencode() on the string. Urlencode will percent-escape any non-printable and a lot of printable characters besides ASCII, so you will see the hexcode of the offending characters instantly. Depending on what you discover, you can act accordingly or update your question to get additional help.


I had a similar problem, also loading emails from CSVs and having issues with "undetectable" whitespaces.

Resolved it by replacing the most common urlencoded whitespace chars with ''. This might help if can't 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 strip the 'normal' space character and that it removes these whitespace chars from anywhere in the string - not just start or end.


In most of the cases a simple strip_tags($string) will work.

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