PHP, trim and the non breaking space

An odd thing occurred today, I was trying to do trim($string) on a string in php, but I kept getting strings back that appeared to have a space at the beginning, the exact thing that trim is supposed to remove. I carefully checked and the space was definitely found when I copied the result out to TextWrangler. Finally I tried substr($string,1) and then I got the answer, the new string started nbsp; the string I was trimming had  , a non-breaking space at the beginning, which trim doesn’t remove. A quick switch to str_replace and now my string doesn’t have a space at the beginning. 🙂

This entry was posted in Internet Stuff, Macintosh. Bookmark the permalink.

5 Responses to PHP, trim and the non breaking space

  1. Bryan says:

    I just had a similar issue. My page was encoded in UTF-8, in which case the encoding for a non-breaking space is 0xC2 0xA0, so I had to use this code to get rid of them (I also had to get rid of carriage return characters, hence the “\n”):

    $string = trim( $string, “\xC2\xA0\n” );

  2. Marc says:

    Hi,

    danke. Hatte das gleiche Problem. Ich hatte mit
    $str = trim(preg_replace('/\s+/', ' ', $str));

    alle Whitespaces entfernt. Dachte ich zumindest. Es waren noch mehrere Leerzeichen vorhanden.

    Mit ord() resultierte es das ASCII Zeichen 160. Das verwirrte mich, weil das ja eigentlich das “á” ist. Erst nach einer weiteren Recherche kam ich auf das non braking space und dann auch auf diesen Blog, der mir dann die Lösung brachte (ISO):
    $str = str_replace("\xA0", " ", $str);

  3. Ken Sanders says:

    Thank you very much Bryan. Your solution was perfect. I couldn’t figure out why my parsed Excel spreadsheet rows were coming out with leading spaces, even after trim, preg_replace, and ltrim. Finally, I converted the first character to a HEX, which resulted in ‘a0’ then found that was a non-breaking space, and here we are.

  4. Buttle Butkus says:

    Replace nbsp (non-breaking space) with regular space like so.

    $value = preg_replace('~\x{00a0}~siu',' ',$value);

    Note that there is a space between the two single quotes after “siu’,” and before “,$value”. You could change that to ” (no space) to remove the nbsp.

  5. mike Q says:

    // — quick solution to remove utf-8 etc. (urlencode) —
    array_walk_recursive($data, function (&$item)
    {
    $item = trim(preg_replace(‘/[\x00-\x1F\x7F\xA0]/u’, “”, $item));
    });

Leave a Reply