preg_replace: how do I strip off all white space and  ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

preg_replace: how do I strip off all white space and  ?

Post by lauthiamkok »

Hi,

how do I strip off all white space and  ?

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

      bold      

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

Code: Select all

$this->content = preg_replace("/\[(.*?)\]\s\s+(.*?)\s\s+\[\/(.*?)\]/", 
				"[$1]$2[/$3]", 
				$this->content);
but it does not work! can you help please?

Many thanks,
Lau
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: preg_replace: how do I strip off all white space and &nb

Post by John Cartwright »

If you are working with html entities, you either need to convert them to their respective characters or adjust your pattern to match the entities.

Try

Code: Select all

$this->content = preg_replace("/\[(.*?)\]\s*(.*?)\s*\[\/(.*?)\]/", "[$1]$2[/$3]", html_entity_decode($this->content));
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

Re: preg_replace: how do I strip off all white space and &nb

Post by lauthiamkok »

John Cartwright wrote:If you are working with html entities, you either need to convert them to their respective characters or adjust your pattern to match the entities.
[/syntax]
yes it is a form input pass from tiny MCE and it generates   when u put spaces in the form input.
John Cartwright wrote:
Try

Code: Select all

$this->content = preg_replace("/\[(.*?)\]\s*(.*?)\s*\[\/(.*?)\]/", "[$1]$2[/$3]", html_entity_decode($this->content));
it works perfectly with this! thanks so much! :D
Post Reply