Top

BBCODE


PHP Code
+ 0 likes
Please Register to submit score.
Bookmark and Share
Average Score  4.0 (of 2 scores)
Date Added  Jun 22, 2007
Last Updated  Jun 22, 2007
Tags  bbcode  huh  nice  php 

Introduction

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

really simple!

Grab the Code

<?
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;
} 
?>

Comments

  (4)  RSS
Hawkee
Comments: 1,039
 
PHP Snippet:  BBCODE
Posted on Jun 22, 2007 11:29 am
You really don't need to do it twice for uppercase letters. Just use str_ireplace to do a case insensitive replace.
Blank
Comments: 6
 
PHP Snippet:  BBCODE
Posted on Jul 7, 2007 4:46 am
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.
mygan
Comments: 1
 
PHP Snippet:  BBCODE
Posted on Sep 2, 2007 3:37 pm
i seee
alanhogan
Comments: 5
 
PHP Snippet:  BBCODE
Posted on Jan 8, 2008 4:25 pm
Also your script is vulnerable to cross-site scripting if you let visitors use it... e.g. [url=javascript:submit_your_cookies_to_my_site();return false;]ooh click me![/url]

Commenting Options

Register or Login to Hawkee.com or use your Facebook or Twitter account by clicking the corresponding button below.

  
Bottom