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(' ',' ', 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); }
// Smilies, an example
if (eregi('[ :', $text)) { if (eregi('[ :)]',$text)) $text = preg_replace("#[ :)]#i", '<img src="/images/smilies/smile.gif">', $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
*_ (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
Random Pattern Background - Generate patterned backgrounds with
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...
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