Search code examples
phptrim

PHP trim() not trimming space


I have run into a strange problem. I am trying to trim a string using php trim(), but it is not working.

Here is my code

echo $deal->{self::DEAL_BUSINESS};
echo '<br>';
echo trim($deal->{self::DEAL_BUSINESS});
echo '<br>';

And here is the output

 Alkaram Studio
 Alkaram Studio

If it is not clear from the output. There is a space in the beginning of both untrimmed and the trimmed string.

In the view source I got this.

&nbsp;Alkaram Studio

Solution

  • Try the following:

    echo trim($deal->{self::DEAL_BUSINESS}, "\xC2\xA0\n");
    

    Or

    $text = preg_replace('~\x{00a0}~siu','',$deal->{self::DEAL_BUSINESS});
    echo $text;