Take the 2-minute tour ×

how do I strip off all white space and &nbsp;?

I have this as a input in a wrapper I create,

[b] &nbsp; &nbsp; &nbsp; bold &nbsp; &nbsp; &nbsp; [/b]

so before turning the text to bold, i want to strip off all white spaces and &nbsp, and turn it into [b]bold[/b],

$this->content = preg_replace("/\[(.*?)\]\s\s+(.*?)\s\s+\[\/(.*?)\]/", 

but it does not work! can you help please?

share|improve this question
The problem is simply that php doesn't recognize &nbsp; as a whitespace character, simply because it is infact just a string of 6 characters. If you want to use regexps for that, you'll need to tell php to explicitely match &nbsp; as well. –  poke Sep 24 '10 at 16:52
where do you get these &nbsp;? may be not to add it at all? –  Your Common Sense Sep 24 '10 at 17:05

6 Answers 6

There is no need for a regex based solution. You can simply use str_replace as:

$input = "[b] &nbsp; &nbsp; &nbsp; bold &nbsp; &nbsp; &nbsp; [/b]";
$input = str_replace(array(' ','&nbsp;'),'',$input);
echo trim($input); // prints [b]bold[/b]
share|improve this answer
This will remove the whitespace outside the [b], too. But I don't know whether that's expected behavior or not. –  NikiC Sep 24 '10 at 16:53
Looking at his sample input/output looks like thats what he wants. –  codaddict Sep 24 '10 at 16:54
Except that the sample doesn't show text outside of the tags. –  poke Sep 24 '10 at 16:58
And what if get one more &nbsp; in next string he is parsing? This is very bad answer –  Vladd Sep 25 at 4:59
up vote 2 down vote accepted

I found another solution

$this->content = preg_replace("/\[(.*?)\]\s*(.*?)\s*\[\/(.*?)\]/", "[$1]$2[/$3]", html_entity_decode($this->content));

thank you so much guys!

share|improve this answer

You can just replace the spaces with empty strings, e.g.

preg_replace("/(?:\s|&nbsp;)+/", "", $this->content, -1)

The -1 causes the replace to hit every instance of the match.

share|improve this answer
$this->content = preg_replace(
share|improve this answer
That wouldn't work either. The original question didn't show the &nbsp; characters that OP wants to be removed. Also your solution will not work at all, since the square brackets are not escaped, and all your grouping doesn't help either. –  poke Sep 24 '10 at 16:54
@poke: I don't get what you want to say. The closing square bracket ] doesn't need to be escaped. All I did was take the OPs regex, remove unnecessary parts and add recognition of &nbsp;. –  NikiC Sep 24 '10 at 17:12
@nikic: Huh? Did you change your answer in between? –  poke Sep 24 '10 at 17:14
@poke: I think you are referring to another answer. I didn't change this one. –  NikiC Sep 24 '10 at 17:21
@nikic: o.O Yeah I think so, my bad then.. confused - Could you slightly change your answer (add a space or something) so I can remove the -1 again? –  poke Sep 24 '10 at 17:23

To give you a complete solution with regular expressions as well:

$this->content = preg_replace( '/\[([a-z]+)\](?:&nbsp;|\s)*(.*?)(?:&nbsp;|\s)*\[\/([a-z]+)\]/', '[$1]$2[/$3]', $this->content );

But in this case you should rather combine the whitespace removal and bbcode transformation to make sure that the tags are correctly:

$this->content = preg_replace( '/\[b\](?:&nbsp;|\s)*(.*?)(?:&nbsp;|\s)*\[\/b]/', '<b>$2</b>', $this->content );
share|improve this answer
thank you so much! this is what I want! thanks! :-)))) –  tealou Sep 24 '10 at 17:52

Another method that would work is:

$this->content = trim(str_replace('&nbsp;','',$this->content));

PHP Manual links:

trim() http://us.php.net/trim

*note: This is assuming $this->content contains only the string posted by OP

share|improve this answer

Your Answer


