How to make a BBcode function ?


Back to programming.


thibault    (2009-04-04)

thibault

How to make a BBcode function ?

How to code a BBcode function that imitates Bulletin Board forums, and that you can change according to your needs. Here is a script that may help you to start.

As a reminder, the aim is to process a text that has been processed already by the htmlspecialchars() PHP function to allow certain HTML functions [bold, font size, color, href, img, videos, smileys and so on...] while avoiding that the user can place HTML tags.

<?

function bbcode($text) 
{
  // To keep the spaces
  $text = str_replace('  ','&nbsp; ', stripslashes($text));

  // To jump a line
  $text = preg_replace("#n#i", '<br>', $text);

  // For pictures, one transform "http", this shouldn't be processed as a url
  if (eregi('[ picture=', $text))  {  $text = preg_replace("#[ picture=http://#i",'[ picture=', $text);  }

  // For videos, one transform "http", this shouldn't be processed as a url, it also transform a Youtube link to access directly to the video
  if (eregi('[ video=', $text))  {  $text = preg_replace("#[ video=http://#i",'[ video=', $text);  $text = preg_replace("#[ video=www.youtube.com/watch?v=#i",'[ video=www.youtube.com/v/', $text);  }

  // For url with tag
  if (eregi('[ /url]', $text))  {  $text = preg_replace("#url=http#i",'url=hzzp', $text);  $text = preg_replace("#url]http#i",'url]hzzp', $text);  $array_test_beg = split('[ url',strtolower($text)) + split('[ URL',strtolower($text));  $array_test_end = split('[ /url]',strtolower($text)) + split('[ /URL]',strtolower($text));  if (count($array_test_beg) == count($array_test_end)) {  if (eregi('[ url=', $text))  $text = preg_replace("#[ url=([ ^[ ]*) ?] ?([ ^]]*) ?[ /url]#i", '<a href="1" rel="nofollow" target="_blank" title="2">2</a>', $text);  if (eregi('[ url]', $text))  $text = preg_replace("#[ url]([ ^[ ]*)[ /url]#i", '<a href="1" rel="nofollow" target="_blank">1</a>', $text);  } }

  // For url without tag
  if (eregi('http://', $text))  {  $text = str_replace("&quot;"," ++^^$$&quot;",$text);  $text = preg_replace("#([ n ])?http://([ a-z0-9-=_%#$~%&;?./()+]+)#i", '1<a href="http://2" rel="nofollow" target="_blank">http://2</a>', $text);  $text = str_replace(" ++^^$$&quot;","&quot;",$text);  }

  // For url with tag
  if (eregi('hzzp', $text))  {  $text = preg_replace("#hzzp#i",'http', $text);  }


  if (eregi('[ ', $text))
  {

  // Colors
  if (eregi('[ /color]', $text))  {  $array_test_beg = split('[ color=',strtolower($text)) + split('[ COLOR=',strtolower($text));  $array_test_end = split('[ /color]',strtolower($text)) + split('[ /COLOR]',strtolower($text));  if (count($array_test_beg) == count($array_test_end)) {  $text = preg_replace("#[ color=red]#i", '<font color="#FF0000">', $text);    $text = preg_replace("#[ color=magenta]#i", '<font color="#FF00FF">', $text);    $text = preg_replace("#[ color=brown]#i", '<font color="#AA0000">', $text);    $text = preg_replace("#[ /color]#i", '</font>', $text);  } }

  // Font size
  if (eregi('[ /size]', $text))  {  $array_test_beg = split('[ size=',strtolower($text)) + split('[ SIZE=',strtolower($text));  $array_test_end = split('[ /size]',strtolower($text)) + split('[ /SIZE]',strtolower($text));  if (count($array_test_beg) == count($array_test_end)) {  $text = preg_replace("#[ size=([ 0-9])]([ ^]]*)[ /size]#i", '<font size="1">2</font>', $text); } }

  // Pictures
  if (eregi('[ /picture]', $text))  {  $array_test_beg = split('[ picture=',strtolower($text)) + split('[ PICTURE=',strtolower($text));  $array_test_end = split('[ /picture]',strtolower($text)) + split('[ /PICTURE]',strtolower($text));  if (count($array_test_beg) == count($array_test_end)) {  if (eregi('[ picture=', $text))  $text = preg_replace("#[ picture=([ ^[ ]*) ?] ?([ ^]]*) ?[ /pic]#i", '<img src="http://1" alt="2">', $text);  } }
 
  // Videos
  if (eregi('[ video=', $text))  {  $text = preg_replace("#[ video=([ a-z0-9-=_%#$~%&;:?./]+)]#i", '<object width="425" height="350"><param name="movie" value="http://1"></param><param name="allowFullScreen" value="true"></param><param name="wmode" value="transparent"></param><embed src="http://1" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350" allowFullScreen="true"></embed></object>', $text);  }

  // Tables
  if (eregi('[ /table]', $text))  {  $array_test_beg = split('[ table',strtolower($text)) + split('[ TABLE',strtolower($text));  $array_test_end = split('[ /table]',strtolower($text)) + split('[ /TABLE]',strtolower($text));  if (count($array_test_beg) == count($array_test_end)) {  $text = preg_replace("#[ table=([ 0-9]+)]#i", '<table width="1">', $text);    $text = preg_replace("#[ table]#i",'<table>', $text);    $text = preg_replace("#[ /table]#i",'</table>', $text);    $text = preg_replace("#[ tr]#i",'<tr>', $text);    $text = preg_replace("#[ /tr]#i",'</tr>', $text);    $text = preg_replace("#[ td]#i",'<td>', $text);    $text = preg_replace("#[ /td]#i",'</td>', $text);  } }

  // Smilies, an example
  if (eregi('[ :', $text))  {  if (eregi('[ :)]',$text)) $text = preg_replace("#[ :)]#i", '<img src="/images/smilies/smile.gif">', $text);  }

 
  if (eregi('[ /b]', $text))  {  $array_test_beg = split('[ b]',strtolower($text)) + split('[ B]',strtolower($text));  $array_test_end = split('[ /b]',strtolower($text)) + split('[ /B]',strtolower($text));  if (count($array_test_beg) == count($array_test_end)) {  $text = preg_replace("#[ b]#i", '<b>', $text);    $text = preg_replace("#[ /b]#i", '</b>', $text);  } }
  if (eregi('[ /i]', $text))  {  $array_test_beg = split('[ i]',strtolower($text)) + split('[ I]',strtolower($text));  $array_test_end = split('[ /i]',strtolower($text)) + split('[ /I]',strtolower($text));  if (count($array_test_beg) == count($array_test_end)) {  $text = preg_replace("#[ i]#i", '<i>', $text);    $text = preg_replace("#[ /i]#i", '</i>', $text);  } }
  if (eregi('[ /u]', $text))  {  $array_test_beg = split('[ u]',strtolower($text)) + split('[ U]',strtolower($text));  $array_test_end = split('[ /u]',strtolower($text)) + split('[ /U]',strtolower($text));  if (count($array_test_beg) == count($array_test_end)) {  $text = preg_replace("#[ u]#i", '<u>', $text);    $text = preg_replace("#[ /u]#i", '</u>', $text);  } }

  if (eregi('[ /strike]', $text))  {  $array_test_beg = split('[ strike]',strtolower($text)) + split('[ STRIKE]',strtolower($text));  $array_test_end = split('[ /strike]',strtolower($text)) + split('[ /STRIKE]',strtolower($text));  if (count($array_test_beg) == count($array_test_end)) {  $text = preg_replace("#[ strike]#i", '<strike>', $text);    $text = preg_replace("#[ /strike]#i", '</strike>', $text);  } }

  if (eregi('[ /code]', $text))  {  $array_test_beg = split('[ code]',strtolower($text)) + split('[ CODE]',strtolower($text));  $array_test_end = split('[ /code]',strtolower($text)) + split('[ /CODE]',strtolower($text));  if (count($array_test_beg) == count($array_test_end)) {  $quote_color = '#FFE0B0';  $text = preg_replace("#[ code]#i",'<div style="color:#333333;background-color:'.$quote_color.';"><code>', $text);    $text = preg_replace("#[ /code]#i",'</code></div>', $text);  } }
  if (eregi('[ /quote]', $text))  {  $array_test_beg = split('[ quote]',strtolower($text)) + split('[ QUOTE]',strtolower($text));  $array_test_end = split('[ /quote]',strtolower($text)) + split('[ /QUOTE]',strtolower($text));  if (count($array_test_beg) == count($array_test_end)) {  $text = preg_replace("#[ quote]#i",'<div style="background-color:#F0F0F0;"><blockquote><font size="-1">', $text);    $text = preg_replace("#[ /quote]#i",'</font></blockquote></div>', $text);  } }

  if (eregi('[ /email]', $text))  {  $text = preg_replace("#[ email]#i",'', $text);    $text = preg_replace("#[ /email]#i",'', $text);  }

  }

  // Email addresses
  if (eregi('@', $text))  {  $text = preg_replace("#([ n ])?([ a-z0-9-_.]+)@([ a-z0-9-_.]+)#i", '12(at)3', $text);  }


  return($text);
}


function bbcode_title($text) 
{
  // No need to jump a line here
  $text = preg_replace("#([ nr]+)#i", ' ', stripslashes($text));

  // Remove tags 
  if (eregi('[ ', $text))  {  $text = preg_replace("#[ (.[ ^]]*)]#i", '', $text);  }

  // Email addresses
  if (eregi('@', $text))  {  $text = preg_replace("#([ n ])?([ a-z0-9-_.]+)@([ a-z0-9-_.]+)#i", '12(at)3', $text);  }
 
  return $text;
}

?>


The first function imitates most of the main BBcode features, while the second removes BBcode tags to build e.g. RSS feeds.

This small script checks the number of opening and closing tags but may react strangely when you include some in some others. Here I replaced "[" by "[ " or [+space to avoid them to be processed by the forum. Of course you can replace "[ " by "[" to retrieve the original code.

You'll notice some tricks to process url with or without tag only once (here with the rel="nofollow" attribute that you may remove). Of course there are hundreds of ways to do it.

Some lines are just examples, you may add as many as smilies or colors as you like, this is only a script that can help you to start to code such a function.

To code a complete forum in PHP, see the next discussion



(more options below)

*_    (2009-08-21 14:49:32)
Confused

How do you tell what BB codes to use =/


*31d042    (2010-02-09 00:29:36)
14 years ago

ceci est un test


hello... ezaezae


See also


ficgs
More websites

You must register to see these links, as this is a collaborative page, then you may change the order of the links by clicking the icons before the titles.



admin
Other websites

The following links might be less relevant, please change their ranks if you find them useful.


 Hypertext Preprocessor (PHP) tutorials - Basic BBCode Function
tutorial index > redirect/24766
  1. Random Pattern Background - Generate patterned backgrounds with
  2. Random Pattern Background - Generate patterned backgrounds with
 ASP :: BBCode Function Or Class
bigresource > ASP BBCode function or class Qr6ktB9L

What do I need to do to enable CDONTS om my macine to make it...
I am trying to create a comments form with html and have it sent...

 Simple BBcode part 1
n1studios > tutorials/php/simple bbcode part 1

Want to get in touch? Got some feedback for me? or a couple...
In this text we have 2 smilies: :) and :D when we are done with our...

 How To Parse Bbcode/scripts
trap17 > index.php/parse bbcodescripts t66879

 Creating a BBCode Parser | UltraMega Blog
ultramegatech > blog/2009/04/creating a bbcode parser

// The matching array of strings to replace matches...
// Convert all special HTML characters into entities to display...

 VB.Net/C# BBCode Function - ASP Free
forums.aspfree > code bank 54/vb net c bbcode function 193480

Public Function BBCode(ByVal strTextToReplace As String) As...
Warburton's the largest independently owned bakery in the UK faced a...

 tables [bbcode] function - Tutorialized
forums.tutorialized > visual basic 118/tables bbcode function 4683

Warburton's the largest independently owned bakery in the UK faced a...
Create the Optimal Architecture for your Critical...

 BB Code Functions [Archive] - vBulletin.org Forum
vbulletin > forum/archive/index.php/t 63113

Have a custom function that is useful? Just send me a link to a...
One bit of info I forgot to add, was that I got the error after...

 Tracker -> IP.Board 3.0 -> BBCODE List Function Broken
community.invisionpower > tracker/issue 19138 bbcode list function broken

Normally, in the past versions, if I start from a clean post and...

 Building a BBCode Function
dzone > links/building a bbcode function

JavaOne 2010 Part 2: Whatever Happened To Christopher Oliver,...
Integrate the 3D Engine Balder into Your Silverlight Applications...
Improve your Life, not your Craft: Why Programmers Shouldn’t Study...

 How To Make Array Variable Global In PHP Function
wallpaperama > forums

Share this page by putting this URL in your comments to other...
To embed this topic, just copy the code from the...

 Function BBCode - TalkPHP
talkphp > news announcements/16 function bbcode

The basic usage of PHPTAL, a XML/XHTML template library for...
Vulnerable methods and the areas they are commonly trusted...

 Text Selection help (bbcode function)
search.code head > F Text Selection help bbcode function 1312798

 PHP News Script - Computer Programmers, Programming Forum | Young Coders
youngcoders > php articles/352 php news script

Computer Programmers, Programming Forum | Young...

 WordPress › Support » BBCode for author description
wordpress > support/topic/255140

 JavaScript: Hide And Show Any Element With CSS
astahost > info.php/javascript hide s

Searching Video's for javascript, hide, show, element, css, simple,...
This textarea will convert to Rich-Text automatically (IE, Firefox,...

 phpBB • Information
phpbb > community/viewtopic.php?f=16&t=511968

The tracker for security issues in phpBB or validated...
View Flash Tutorials that help you with the use of...

 This is an interface to add/delete/modify bbcode, and handle parsing - BBCode - Seditio Forge
seditioforge > plugins/miscellaneous/bbcode i100

Replace with (functions are sperated with /* ------------------ */...
This is an interface to add/delete/modify bbcode, and handle...
/* removed the core of the function so its not so huge...

 Make BBCode work with Codefilter (and Youtube embedding) | drupal.org
drupal > node/166167

 My PHP bbcode function - Neowin Forums
neowin > forum/index.php?s



Response  
 

Guest name   (option)     Register
Please sum : 374 + three  




Trackbacks : If you talked about this article in your blog or website, you may instantly get a backlink 
There's no trackback at the moment.