Misc Links
Forum Archive
News Archive
File DB
 

Ads
 

Advertisement
 

Latest Forum Topics
wow 56 k modems are
Posted by Red Squirrel
on Oct 14 2013, 11:52:23 pm

I Need A Program
Posted by rovingcowboy
on Sep 23 2013, 5:37:59 pm

having trouble witn lan
Posted by rovingcowboy
on Sep 23 2013, 5:40:56 pm

new problem for me
Posted by rovingcowboy
on Sep 23 2013, 5:54:09 pm

RBC Royal Bank
Posted by Red Squirrel
on Aug 13 2013, 6:48:08 pm

 

BBcode editor: PHP
Regular expressions and all things nice
By Jonathan Street


Now that we've converted the code tags it's safe to convert everything else. Luckily for us this can all be done at once.



Firstly we create two arrays. $bbcode_regex contains all the regular expression we're looking for. $bbcode_replace contains what we want to replace them with. Next we apply ksort to both these arrays to make sure we start inspecting each array in the right place. This is probably unnecessary but I think it's good practice. Using preg_replace we look for text matching the regular expressions and replace it with their replacements.

Now on to regular expressions: incredibly useful but on first glance incomprehensible. As an example we'll take number 4 (one of the more complex expressions) and break it down.



There are certain special characters available for use in regular expressions which is really where the power lies. However, if we just want to use the standard meaning for a character we can 'escape' its special meaning with a backslash. If we strip out these backslashes we get the following.



We also need to define the start and end of the regular expression. This is simply done by putting the same character, in this case the forward slash, at the beginning and end of the expression. We can also add certain letters on to the end of the expression which have certain meanings. Let's strip all that away.



This is looking a lot like the bbcode we are familiar with. The only difference remaining are the two occurances of (.+?). The period just means any character and the plus sign means one or more. This will therefore match whatever the user puts between the tags. Why the question mark? Ordinarily that plus sign is greedy meaning that if the user uses a certain bbcode tag twice it will ignore the first closing tag and the opening tag of the second. It will grab more of the post than we want it to grab. This is what we mean by greedy. Adding the question mark makes it less greedy so it will recognise that there are two identical tags and treat them accordingly.

I mentioned before about adding letters onto the end of the regular expression. Here I use an s. This just makes sure that tags encompassing multiple lines of text doesn't present a problem.

Hopefully that explains everything except from how we get the text between the tags out. Going back to (.+?) we see it is enclosed in brackets. Brackets signify that we want this text available later. Text enclosed in brackets is put into variables that can be used in the replacement. The replacement for the above regular expression is as follows.



This is just standard html apart from $1 and $2. These are the variables where our text is stored. $0 contains the entire string. $1 contains the text from the first set of brakets and $2 contains the text from the second set of brackets.

That's it for replacing the bbCode tags. Apart from creating the regular expressions there isn't much to it.

The final thing that needs to be done is replace the new lines with br tags and then echo the post to the browser.



nl2br is a nice little function that does exactly what it says it will do. It converts new lines to br tags.

Hopefully you should now be able to easily handle bbCode submitted by your visitors as well as making it easy for them to submit bbCode in the first place. In most cases the bbCode replacements label the differenct aspects of the post using classes. This should make it easy for you to match the output of these elements with the design of your site using cascading style sheets. Good luck and if you have any questions feel free to ask on the forum.




spacer
29188 Hits Pages: [1] [2] [3] 8 Comments
spacer


Latest comments (newest first)
Posted by unokpasabaxaki on March 03th 2008 (20: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 (07: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 (21: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 Red Squirrel on October 10th 2005 (20: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
Posted by MikeDB on October 10th 2005 (16:04)
Yes yes I dont nkow why im even answering to this post but... Yes.
spacer
View all comments
Post comment


Top Articles Latest Articles
- What are .bin files for? (669062 reads)
- Text searching in linux with grep (161180 reads)
- Big Brother and Ndisuio.sys (150471 reads)
- PSP User's Guide (139547 reads)
- SPFDisk (Special Fdisk) Partition Manager (117240 reads)
- How to Use MDADM Linux Raid (188 reads)
- What is Cloud Computing? (1225 reads)
- Dynamic Forum Signatures (version 2) (8769 reads)
- Successfully Hacking your iPhone or iTouch (18714 reads)
- Ultima Online Newbie Guide (35906 reads)
corner image

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