Jump to content
xisto Community
Sign in to follow this  
Unholy Prayer

Use Bb Code On Your Site! Just like on forums

Recommended Posts

To use this you must have PHP support on your server. Just use this code:

<?php$content = "Hello, World!";$html = array('[b]', '[/b]', '[i]', '[/i]', '[u]', '[/u]');$replacements = array ('<b>', '</b>', '<i>', '</i>', '<u>', '</u>');$content = str_replace($html, $replacements, $content);?>

This code can be very useful. It can be used for word filters to block the use of bad words on your site or for emoticons.

Share this post


Link to post
Share on other sites

str replace is bad for bbcode especially when it comes to urls etc.

 

If you use preg_replace, you can get php to check to make sure it actually has an ending. That way if sombody leaves a tag open, it dosn't parse it out. They would have to close it.

 

Here's the bb code i use on the forums i coded.

 

$main_search=array('/\[b\](.*?)\[\/b\]/is','/\[i\](.*?)\[\/i\]/is','/\[u\](.*?)\[\/u\]/is','/\[img\](.*?)\[\/img\]/is','/\[url\=(.*?)\](.*?)\[\/url\]/is','/\[url\](.*?)\[\/url\]/is','/\[color\=(.*?)\](.*?)\[\/color\]/is','/\[code\](.*?)\[\/code\]/is');$main_replace=array('<strong>$1</strong>','<em>$1</em>','<u>$1</u>','<img src="$1" style="max-width: 100%" border="0">','<a href="$1">$2</a>','<a href="$1">$1</a>','<font color=$1>$2</font>','<div style="WIDTH: 100%; OVERFLOW: auto"><pre>$1</pre></div>');$str=preg_replace ($main_search, $main_replace, $str);

Str_replace is good for smilies though!

Share this post


Link to post
Share on other sites

Ok! Do they both work though? I used another tutorial here that used preg_replace. They also said it was more better than str_replace. Unholy, you might want ot take his advice!

Share this post


Link to post
Share on other sites

Ok! Do they both work though? I used another tutorial here that used preg_replace. They also said it was more better than str_replace. Unholy, you might want ot take his advice!

More better? lol

 

If you use preg_replace you'll get a much more professional bb parser. It would be more like Xisto's bb. This would be the effect you would get:

 

if this board used str_replace to parse bb code, the rest of this post would be bold because the bold tag would be parsed and replaced into <b> no matter what.

Share this post


Link to post
Share on other sites

The problem with using str_replace for tags like img is you would have to have "http://forums.xisto.com/ replace to <img src=" and [ /img]http://forums.xisto.com/ replace to ">. Now if you think about it, if you use that, anybody could easily break your forums by just starting an image tag without ending it. If you write <img src=" in the middle of your code, it's not going to end untill it sees another quote and another greaterthan. If your site uses tables, this could really screw up the site.

 

P.S.

Along with emoticons, str_replace is really good for disabling HTML.

 

$string=str_replace(array("<",">"),array("<",">"),$string);

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.