Sunday, November 10, 2019

php - Whitespace in a database field is not removed by trim()

you have to detect these "whitespace" characters first. if it's some HTML entity, like   no trimming function would help, of course.


I'd suggest to print it out like this


echo urlenclde($row['field']);

and see what it says


Well as its A0 (or 160 decimal) non-breaking space character, you can convert it to ordinal space first:


$str = urldecode("%A0")."bla";
var_dump(trim($str));
$str = str_replace(chr(160)," ",$str);
$str = trim($str);
var_dump($str);

and, ta-dam! -


string(4) " bla"
string(3) "bla"

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...