954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

preg_replace regular expression problem with bbcode

Ok so I am trying to create some BB code from scratch, and my big problem is the \S and \s and .* are very greedy. For example, when I try to use bb code on something like this:

[php]$text = "abcdefg !@#$%^&* 0123456789";
$text = preg_replace("/\[b\](.*)\[\/b\]/","\\1",$text);
//same thing:
//$text = preg_replace("/\[b\]([\s\S]+)\[\/b\]/","\\1",$text);
echo $text;[/php]
i get my output to look like this:
abcdefg{/b} !@#$%^&*() {b}0123456789
without the { } around the bold markers (I put them there because i didn't know how to tell daniweb to print it out as a literal). But now what I want it to look like is this:
abcdefg !@#$%^&* 0123456789
where the text in the middle is not bold

I know what I have to do to get fix this, but I don't know how to implement it, what is should be is {b} then any text except for another {b} and then end on a {/b} I some how have to put a carat ^ in there but dont know how to do it in this example

can anyone help?

paradox814
Posting Whiz
351 posts since Oct 2004
Reputation Points: 13
Solved Threads: 4
 

Use the lazy operator, the question mark:

"#\[b\](.*?)\[/b\]#"
// hashes make better delimiters when you're using slashes
Ooble
Light Poster
44 posts since Oct 2005
Reputation Points: 12
Solved Threads: 6
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You