PHP trim() not trimming space-ThrowExceptions

Exception or error:

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
How to solve:

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;

Leave a Reply

Your email address will not be published. Required fields are marked *