matching any character including white space characters...

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

matching any character including white space characters...

Post by Dave2000 »

Code: Select all

function bb_code($text) 
{
	$bbcode_to_from = array(
	'/\[b\](.+)\[\/b\]/i' => '<b>$1</b>', 
	);

	foreach ($bbcode_to_from AS $from => $to)
	{
		$text = preg_replace($from, $to, $text);
	}
	
	return $text;
}

$text = '[b]hel
lo[/b]';

Echo = bb_code($text);
The above example wont work. Despite a "." supposedly representing any character, i eventually found out it doesn't represent white space characters. How could i change the above to include white space characters? I have tried many things, like...

Code: Select all

'/\[b\]([.|/s+)\[\/b\]/i'
but none seem to work :(
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Code: Select all

'/\[b\]([.\s+])\[\/b\]/i'
?
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Post by Dave2000 »

Hi Kieran. That doesn't work :( I also tried it with the + outside the square brackets :?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I think what you're probably looking for is:

Code: Select all

(?:\[b\])(.*?)(?:\[\/b\])
...then $1 will be the inner-text.

Check out: http://www.cuneytyilmaz.com/prog/jrx/
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Ahem. "." represents white-space characters. It does not, without modification, match new-line characters because the default action is on a per line basis.

The DOT_ALL flag or "s" pattern modifier may be of interest.
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Post by Dave2000 »

The "s" modifier does the trick nicely. Thank you :)
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

:oops:

Nice catch feyd!
Post Reply