Simple bbCode [TUT] - Printable Version +- Support Forums (https://www.supportforums.net) +-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87) +--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18) +---- Forum: PHP The Hypertext Preprocessor (https://www.supportforums.net/forumdisplay.php?fid=21) +---- Thread: Simple bbCode [TUT] (/showthread.php?tid=1676) |
Simple bbCode [TUT] - Gaijin - 10-17-2009 What is bbCode http://en.wikipedia.org/wiki/BbCode Wrote:Bulletin Board Code or BBCode is a lightweight markup language used to format posts in many message boards. In this tutorial I will show you a simple way of parsing bbCodes using Regular Expressions with the function preg_replace(). The function parses only b, url, color tags, I hope to get you to expand it your self to do more. Functions - preg_replace(mixed $pattern, mixed $replacement, mixed $subject [, int $limit = -1 [, int &$count]]) Start For our bbCode parser all we need is one simple function. PHP Code: <?php The first parameter of the preg_replace() function is used for matching pattern. Code: /\[b\](.*?)\[\/b\]/ In the first we are lookgin for Bold text bbCode, we use the regular expression (.*?) to match every thing between opening and closing bbTag, the match is then saved to "$1" variable. In the second we are lookgin for link replacement, in this tag we use 2 regular expressions, and they are saved in the row of their use, $1 would be the URL of our link and $2 the name of the link. We use the ([^ ]+).* expression to match everything besides white space. And in the third we want to allow changing of font color, we use ([[:alnum:]]{6}?).* to match alpha numeric characters with the total length of 6 characters. That's it, simple isn't it? All you have to do now is pass some text with bbCode in it to our function. PHP Code: print bbCode("This is [b]bold[/b] text with the [color=33cc66]green[/color] color and a link to [url=http://www.php.net]PHP main page[/url]"); A list of mostly used bbCodes can be found by following the link below. Thanks to MreGSX Wrote:http://www.supportforums.net/misc.php?action=help&hid=7 Thank you for learning. Source (Click to View) RE: Simple bbCode [TUT] - Viciousness - 10-17-2009 Good tutorial. There's a simple list of BBcode commands here that you may want to add. RE: Simple bbCode [TUT] - Gaijin - 10-17-2009 (10-17-2009, 08:50 AM)MreGSX Wrote: Good tutorial. There's a simple list of commands here that you may want to add. Thanks, I didn't know what to do so I've killed 15 minutes writing this., it's simple but it does the job. And thanks for the link will add it. RE: Simple bbCode [TUT] - zone - 11-08-2009 Very good tut. I use this simple function whenever I like to parse the BBcode. PHP Code: <? Hopefully this is also an helpful idea RE: Simple bbCode [TUT] - Gaijin - 11-08-2009 (11-08-2009, 07:44 PM)zone Wrote: Very good tut. Thanks, That's a nice code, but I would suggest you to use RegEx it would shorten your function and give you more control. RE: Simple bbCode [TUT] - zone - 11-08-2009 (11-08-2009, 07:49 PM)Master of The Universe Wrote: Thanks, That's a nice code, but I would suggest you to use RegEx it would shorten your function and give you more control. That's very true, but seriously, I am a fresh student of php I am learning its detailed functions. Hopefully I will add this to my to do list to learn more about your RegEx() Thanks Ninja Geek, be happy RE: Simple bbCode [TUT] - Gaijin - 11-08-2009 (11-08-2009, 07:58 PM)zone Wrote: That's very true, but seriously, I am a fresh student of php I am learning its detailed functions. Hopefully I will add this to my to do list to learn more about your RegEx() I have few tutorials in here for Beginners, and Tim's 101 is also good. And here you can read about regex http://www.webcheatsheet.com/php/regular_expressions.php RE: Simple bbCode [TUT] - zone - 11-08-2009 Thanks for the link Ninja, it is help ful really, I bookmarked the site and will learn some of the basics from there. RE: Simple bbCode [TUT] - Bencori - 05-25-2011 Thank you. Found it useful and it replied to my question. RE: Simple bbCode [TUT] - Carbon Host - 08-02-2011 great tutorial |