Misc Links
Forum Archive
News Archive
File DB
 

Ads
 

Advertisement
Your text ad could be here for only $12/month!

Click here for more info!

 

Latest Forum Topics
Disable UAC in windows Vista/7/2008
Posted by Red Squirrel
on Jun 24 2009, 1:52:50 pm

IE 8 update error
Posted by rovingcowboy
on Jun 15 2009, 8:18:40 am

77' Chevy Nova
Posted by MikeDB
on Jun 25 2009, 4:36:46 pm

Bought a house
Posted by Triple6_wild
on Jun 15 2009, 3:22:07 am

computer desk chairs/office chairs
Posted by Onykage
on May 08 2009, 9:57:00 am

 



Send spam to: website@xeonlive.com nick@xeonlive.com georgiapeach1241@aol.com
BBcode editor: PHP
Regular expressions and all things nice
By Jonathan Street


This is the second article looking at how to allow your visitors to post content using bbCode. While the previous article looked at the client-side processing this article will deal with how to treat bbCode formatted text once it reaches your server. If you want to learn how to make it easier for your visitors to use bbCode check out the first article here.

Once your visitor posts their content you have several options. You can store it as bbCode and then convert it to html every time it is views. You can convert it to html when it is first submitted, store this and then just display it as-is when ever it is viewed. You can store it as bbCode AND convert it to html when first submitted and store this also. The best option for you will depend on your needs.

If it is likely that your visitors will wish to modify their submissions or that other users might want to quote them then you're best storing it as bbCode. Whether you store just the bbCode or the bbCode and html will mean considering the facilities available to you. If your server has plenty of spare processing power available but you're limited in storage capacity just store the bbCode. If your server is already working at it's limits but you have plenty of storage capacity still available store both. If it is just going to be posted once and then left just storing the html should be fine and will save on processing time.

Because you do have multiple options available to you, both in terms of what you store and where you store it, I won't be discussing how to store your visitors content. In this article I'll just be looking at creating a function in php that will convert bbCode text into html formatted text. How you then use this is up to you.


Next Page
spacer
23692 Hits Pages: [1] [2] [3] 14 Comments
spacer


Latest comments (newest first)
Posted by unokpasabaxaki on March 03th 2008 (19:44)
I also had a "blank page" problem, but I've resolved it.
When I saw the function returned an empty string, I checked the code, and after copying outside of the function, I found out that it failed because I had missed the "code" part. I had:
CODE
$post_abridged = chop($post_no_html);
$bbcode_regex = [...]
$post_bbcode_treated = preg_replace($bbcode_regex, $bbcode_replace, $code_treated);

I changed "$code_treated" to "$post_abridged", because I hadn't defined the "$code_treated" variable at missing the "code" part.
So if you find that the script returns a blank page, make sure you haven't missed any step; and if you have, change the variable name.

[Sorry about my bad English: I'm Spanish and I'm not very confident with it]

spacer
Posted by AdRock on September 09st 2007 (06:12)
I have worked out why it wouldn't work before by following the article again but I still can't output what is entered in the form. All I get is a blank page
spacer
Posted by AdRock on August 08th 2007 (20:12)
I have got the form with the BBcode working fine now after reading the article properly but I am now stuck on converting teh BBcode into HTML.

First thing...is the file supposed to be called bbcode.php like referred to in the first part of the article?

Second thing....is there a complete listing of the bbcode.php? I have gone through the second part and put the code together but when i try and preview what I have done...it just echoes this

QUOTE
function output_post ($post) { //Make safe any html $post_no_html = htmlspecialchars($post); //Make sure there is no whitespace at the end of the message //It's conceivable that the user will start their message with whitespace $post_abridged = chop($post_no_html); //Callback function for preg_replace_callback below function convert_for_html ($matches) { $regex[0] = "["; $regex[1] = "]"; $replace[0] = "["; $replace[1] = "]"; ksort($regex); ksort($replace); $treated = str_replace($regex, $replace, $matches[1]); $output = 'Code:
' . $treated . '
'; return $output; } //Convert code tags $code_treated = preg_replace_callback( "/\[code\](.+?)\[\/code\]/s", "convert_for_html", $post_abridged); //Arrays for the bbCode replacements $bbcode_regex = array(0 => '/\[b\](.+?)\[\/b\]/s', 1 => '/\[i\](.+?)\[\/i\]/s', 2 => '/\[u\](.+?)\[\/u\]/s', 3 => '/\[quote\](.+?)\[\/quote\]/s', 4 => '/\[quote\=(.+?)](.+?)\[\/quote\]/s', 5 => '/\[url\](.+?)\[\/url\]/s', 6 => '/\[url\=(.+?)\](.+?)\[\/url\]/s', 7 => '/\[img\](.+?)\[\/img\]/s', 8 => '/\[color\=(.+?)\](.+?)\[\/color\]/s', 9 => '/\[size\=(.+?)\](.+?)\[\/size\]/s'); $bbcode_replace = array(0 => '$1', 1 => '$1', 2 => '$1', 3 => 'Quote:
$1
', 4 => '$1 said:
$2
', 5 => '$1', 6 => '$2', 7 => '', 8 => '$2', 9 => '$2'); ksort($bbcode_regex); ksort($bbcode_replace); //preg_replace to convert all remaining bbCode tags $post_bbcode_treated = preg_replace($bbcode_regex, $bbcode_replace, $code_treated); //Convert new lines to
$post_with_br = nl2br($post_bbcode_treated); echo $post_with_br; };


How do i get this working?

spacer
Posted by Dragon on November 11th 2005 (21:25)
How about something like this?

Simple to use, wherever a forum like this will allow such code to be used...

BBCode
HTML code
XHTML code


Im using BBCode, for this post, with the little program I posted in first sentence. wink.gif

spacer
Posted by Red Squirrel on October 10th 2005 (19:30)
Hey sorry to the person who just posted and got hit with the spam bot. really not sure why it snapped. Must be the word compliant, thats often in spam.

Feel free to register as the spam bot validation wears off after some posts.

spacer
View all comments
Post comment


Top Articles Latest Articles
- What are .bin files for? (534428 reads)
- Big Brother and Ndisuio.sys (134470 reads)
- PSP User's Guide (114988 reads)
- SPFDisk (Special Fdisk) Partition Manager (84305 reads)
- Text searching in linux with grep (82031 reads)
- Dynamic Forum Signatures (version 2) (2178 reads)
- Successfully Hacking your iPhone or iTouch (9679 reads)
- Ultima Online Newbie Guide (16544 reads)
- BBcode editor: PHP - The sensible approach (14013 reads)
- The Hitch Hikers guide to "the mouse" (12403 reads)
corner image

This site best viewed in a W3C standard browser at 800*600 or higher
Site design by Red Squirrel | Contact
© Copyright 2009 Ryan Auclair/IceTeks, All rights reserved