tags:

views:

38

answers:

1

I'm working on a project right now that is a shoutbox. Since I have disabled all HTML as people would most likely forget to close a tag ans screw up everything, I wanted to make a different form of adding some flair for people to use in their posts.

What I want to to is setup a regular expression in PHP to add tags around text. Here is an example of what I want to do...

If a person types "[Hello!] How is {everyone}?" it would display as such...
"Hello! How is everyone?"

The text in between the [ ] brackets would become bold and the text in between the { } brackets would be italics.

Does anyone know how to write a regular expression for this?
Thanks!

+4  A: 

One way to do it would be:

$str = preg_replace('~\[([^\]]*)\]~', '<b>\\1</b>', $str);
$str = preg_replace('~{([^}]*)}~', '<i>\\1</i>', $str);

As seen on codepad.

That being said, I disapprove of creating yet another mark up language. Like BBCode, it's evil.

BBCode is a markup language invented by lazy programmers who didn't want to filter HTML the proper way. As a result, we now have a loose "standard" that's hard to implement. Filter your HTML the right way:

http://htmlpurifier.org/

NullUserException
+1 for the sentiment. Reinventing the wheel is a waste of time.
EboMike
You are very smart when it comes to regular expressions haha. Worked like a charm!!! Thank you very much for your help!
BigRossLabs