FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP - BBcode Parsing





mattd8752
I wrote the code for a php based bb code parser function. I hope you all like it. I commented on every single line of code. I will explain how to parse
Code:
<?php  //Defines the start of a block of php code.
//Anything with // infront of it is a comment and is only readable when editing.
//BBCode Parser

//Create the function.
//Function tells php that this is creating a function.
//BB follows to tell php that bb is the name of the function.
//Text is what would go in the brackets of the command.
function bb($text)
{ //This establishes the start of the function commmand.
$array=array(  //This creates an array with the name $array.

"[b]" => "<b>",  //This sets the value of [b] to <b> so if you looked under [b] in the array you would find <b>.

"[/b]" => "</b>",  //This sets the value of [/b] to </b>

"[i]" => "<i>",  //This sets the value of [i] to <i>

"[/i]" => "</i>",  //This sets the value of [/i] to </i>

"[u]" => "<u>",  //This sets the value of [u] to <u>

"[/u]" => "</u>",  //This sets the value of [/u] to </u>

"[center]" => "<center>",  //This sets the value of [center] to <center>

"[/center]" => "</center>",  //This sets the value of [/center] to </center>

"[big]" => "<h1>",  //This sets the value of [big] to <h1>

"[/big]" => "</h1>",  //This sets the value of [/big] to </h1>

"[nl]" => "<br>",  //This sets the value of [nl] to <br>

);  //This is the end of the data.
$newtext = str_replace(array_keys($array), array_values($array), $text);  //This takes the name of the components like
//[b] and the others which I showed to their value.  It puts $newtext as a varuible to run this.
return $newtext;  //$newtext is sent back to the function requesting php.  It also runs the str_replace which was set to
//this varuible.
} //The function is now ended.
?>  //Defines the end of a block of php code.


I will explain how to use this with a function to parse data and write it to a file, or to get data from a database, parser it, then write it file in part 2 which I will be working on soon. This is good to limit what html people can use.
I will write a new tutorial when my donatometer reaches 100, send me a transfer of any amound of frih$ and each frih$ give me 1 donatometer point.



Donatometer
Everytime I recieve a donation with a pm attached saying "donatometer, I record one more point, every 5 points you get an extra - deleted, I will expand the chart every 25 points. The more you donate the more chances you have towards me releasing a new tutorial. I gave 5 points free.

Thanks to:
Mathius - 10 frih$
N00Bie4Life turned this into a sticky, I gave some extra points for that.

|||0
|||5
|||10
|||15
|||20
|||25
|-|30
|-|35
|-|40
|-|45
|-|50
|-|55
|-|60
|-|65
|-|70
|-|75
|-|80
|-|85
|-|90
|-|95
|-|100
mattd8752
Part 2 is almost ready... I hope I get enough donations...
n0obie4life
If I sticky this, can it be counted as a donation Razz

(I;ve been doing that for most tutorials anyway (sticking quality and self-written tuts))
mattd8752
Why not, sure, I will put it up for 10frih$... Now, I gotta get writing that turorial. Thanks though.

Sample of part 2:

Quote:
In this tutorial I will be using a code which you can make yourself for a full member system using a few tutorials from techtuts.com : Part 1: http://techtuts.com/?view=tutorials&act=tutorial&id=8 & Part 2: http://techtuts.com/?view=tutorials&act=tutorial&id=10 & Part 3: http://techtuts.com/?view=tutorials&act=tutorial&id=12 & finally Part 4: http://techtuts.com/?view=tutorials&act=tutorial&id=35. Those are extremely good tutorials, I don't think you will have any questions, but if you do feel free to ask there or here. I will try my best to help you. Setting up that code should take no more than an hour. I will now teach you how to make a news script that uses the bbcode parser, and tells you who wrote each article and how allows users to comment on the articles. So to begin...
Also a simple news script which will help me, however I won't use any of the script from it except for queries, can be found here: http://techtuts.com/?view=tutorials&act=tutorial&id=21 I hope this encourages you to donate towards getting this tutorial.
The World is Yours
I'll donate 50 frih$ to this tutorial if it is still an open project....
Related topics
PHP Tutorials
Can't get strings to output correctly
Error on parsing smily pack, PhpBB
BB Codes
code showoff: LF2 Extendable BBcode parser(LF2EBB)
uncoded code ! :P
BB Code [EDIT: Offering 150 FRIH$]
PHP RSS Parsing
Undo htmlspecialchars
Parse feed tags/urls using rss2html
+BBCode [php]...[/php]
Is it a bad practice?
PHP XML Parsing
.htaccess issues with & and ? characters in title
Reply to topic    Frihost Forum Index -> Miscellaneous -> Tutorials

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.