- JeremysFilms
- Sunday, July 23rd, 2006 at 3:07:15pm UTC
- Here's how I make my own "bbcode" and use it.
- 1) Run htmlentities() on the message being posted to make it safe before it's added to a database.
- 2) Insert this message that has been altered with htmlentities() into the database.
- 3) When you are outputting the messages to the page, that's when you want to run something to replace the bbcode with html, this way, if you ever want to alter what a specific tag does, you won't have to go through every post and change the html there, since it'll just have the bbcode in there.
- Just use a preg_replace to replace the bbcode with html during output, it's pretty simple:
- __________________________________________________
- $pattern=array(
- '/\[b\](.*)\[\/b\]/i',
- '/\[i\](.*)\[\/i\]/i',
- '/\[u\](.*)\[\/u\]/i',
- '/\[color=(.*)\](.*)\[\/color\]/i'
- );
- $replace=array(
- '<b>\\1</b>',
- '<i>\\1</i>',
- '<u>\\1</u>',
- '<font color=\"\\1\">\\2</font>'
- );
- preg_replace($pattern,$replace,$stringToOutput);
- __________________________________________________
- $stringToOutput would be the string that is being outputted from the database.
- Example snippet:
- while($row = mysql_fetch_array($result)){
- echo preg_replace($pattern,$replace,stripslashes($row['message'])).'<br /><br />;
- }
Update the Post
Either update this post and resubmit it with changes, or make a new post.
You may also comment on this post.
Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.