By mygan on Jun 22, 2007

This function let u use like ['u]poopoo['/u] (without ' ).
this will result in = poopoo

really simple!

function bbcode($text){
   $text = str_replace('<', '&lt;', $text);
   $text = str_replace('>', '&gt;', $text);
   $text = str_replace('"', "&quot;", $text);

   $text = str_replace('[b]', '<b>', $text);
   $text = str_replace('[/b]', '</b>', $text);
   $text = str_replace('[i]', '<i>', $text);
   $text = str_replace('[/i]', '</i>', $text);
   $text = str_replace('[u]', '<u>', $text);
   $text = str_replace('[/u]', '</u>', $text);
   $text = str_replace('[url=', '<a href="', $text);
   $text = str_replace('[/url]', '</a>', $text);
   $text = str_replace('[img]', '<img src="', $text);
   $text = str_replace('[/img]', '">', $text);
   $text = str_replace('[c]', '<center>', $text);
   $text = str_replace('[/c]', '</center>', $text);
   $text = str_replace(']', '">', $text);

   $text = str_replace('[B]', '<b>', $text);
   $text = str_replace('[/B]', '</b>', $text);
   $text = str_replace('[I]', '<i>', $text);
   $text = str_replace('[/I]', '</i>', $text);
   $text = str_replace('[U]', '<u>', $text);
   $text = str_replace('[/U]', '</u>', $text);
   $text = str_replace('[URL=', '<a href="', $text);
   $text = str_replace('[/URL]', '</a>', $text);
   $text = str_replace('[IMG]', '<img src="', $text);
   $text = str_replace('[/IMG]', '">', $text);
   $text = str_replace('[C]', '<center>', $text);
   $text = str_replace('[/C]', '</center>', $text);
   $text = str_replace(']', '">', $text);

   $text = nl2br($text);
   $text = trim($text);

   return $text;


Sign in to comment.
alanhogan   -  Jan 08, 2008

Also your script is vulnerable to cross-site scripting if you let visitors use it... e.g. [ooh click me!](javascript:submit_your_cookies_to_my_site();return false;)

mygan   -  Sep 02, 2007

i seee

Blank   -  Jul 07, 2007

just a suggestion, but to decrease the size of the script, consider sticking the search and replacements into arrays. also, just using str_replace (or as Hawkee suggested, str_ireplace), means that you aren\'t actually validating the input, meaning that the user could possibly break up the format of the output page by adding too many closing or opening tags. A preg_replace would be better, that way if there are any mismatches, they aren\'t replaced at all.

Hawkee   -  Jun 22, 2007

You really don\'t need to do it twice for uppercase letters. Just use str_ireplace to do a case insensitive replace.

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.