Taking tutorial submissions! Please email them to dave[AT]icemelon[DOT]com for review. Thanks!

Latest Tutorials
IceMelon IM: Add IM Functionality to phpBB [Misc] Want to add site-wide member IM to your phpBB community? Sure you do. Simple step-by-step instructions inside.

IceMelon IM: Add IM Functionality to vBulletin [Misc] Want to add site-wide member IM to your vBulletin community? Simple step-by-step instructions inside.

Crawl Your Site for PageRanks [PHP] So you know your site has a PageRank of 8. What about the PRs of all the inner pages? This tutorial will teach how you to automate the process of grabbing all these PRs.

Create CAPTCHA images [PHP] Worried about bots? Then create CAPTCHA images to prevent bots from taking advantage of your site.

Convert SHN and FLAC files to MP3 [Misc] Low on disk space and have an insensitive ear? Here are some steps to converting SHN and FLAC files to MP3 format.

[View All]

Become a sponsor for $15/month. Link is sitewide - PR5 homepage, 20+ PR4 pages, 90+ PR3 pages. Email dave[AT]icemelon[D0T]c0m.

Awesome Tutorials

Create Your Own BBCode
By dave

Online forums (message boards, bulletin boards, or whatever you wanna call them) often support BBCode. Not sure what I'm talking about? Here's an example:
[url=http://www.nutang.com]NuTang Weblog Community[/url]
Should be converted to..
<a href="http://www.nutang.com">NuTang Weblog Community</a>

In this tutorial, I'll teach how, using PHP, you can parse a block of text and convert the BBCode into HTML. The function in the spotlight here is preg_replace. To fully understand its use, you should be familiar with regular expressions. If you aren't, just skim my regular expressions tutorial [http://www.icemelon.com/tutorials/1/Regular_Expressions.htm].

First, let's lay down a few assumptions. I'll assume the text you wanna parse is stored in the variable $stuff. Now, let's just get down to the code:
// convert [url=URL]link_title[/url]
$pattern[] = '/\[url=(.*?)\](.*?)\[\/url\]/i';
$replace[] = '<a href="$1">$2</a>';

// convert [url]url_link[/url]
$pattern[] = '/\[url\](.*?)\[\/url\]/i';
$replace[] = '<a href="$1">$1</a>';

// convert [img]image_link[/img]
$pattern[] = '/\[img\](.*?)\[\/img\]/i';
$replace[] = '<img src="$1">';

// convert [b]text[/b]
$pattern[] = '/\[b\](.*?)\[\/b\]/i';
$replace[] = '<b>$1</b>';

// convert [code]CODE[/code]
$pattern[] = '/\[code\](.*?)\[\/code\]/i';
$replace[] = '<xmp>$1</xmp>';

$html = preg_replace($pattern, $replace, $stuff);

The above code supports the following BBCode tags: [url], [img], [code], & [b]. Let's just go over one example, because as you can see, they're all variations of the same structure. Let's look at [b], which converts text to be boldfaced:
// convert [b]text[/b]
preg_match('/\[b\](.*?)\[\/b\]/i', '<b>$1</b>', $stuff);
Remember, stuff that fits the expression within the parentheses will be stored in $1 (then $2, $3, etc., if applicable). In the above example, we only have one set of parentheses, so we only need to worry about $1. The $1 will store the text to be boldfaced. Further note, a lot of characters need to be escaped (using '\'). (Again, check out details about the function here: preg_replace).

Questions
  • What's with the "(.*?)" ? Why not just use "(.*)" ? I actually don't quite understand that myself, but if you try the second version, things will not work out the way you want. You will lose A LOT of matches.
  • Can't I just use str_replace? Good observation. You certainly can, and it would be more efficient. However, preg_match was used in the examples, because it is more powerful. You can manipulate your "$stuff" a lot more, especially with this function: preg_replace_callback.

    Speaking of str_replace, you should use this function to convert your smilies. For instance:
    // converts ;-) to a wink-smilie
    $html = str_replace(';-)', '<img src=images/smilies/wink.gif>', $html);

    More Related Functions
  • preg_match
  • preg_match_all
  • substr_replace
  • str_ireplace
  • ereg_replace
  • preg_split

    Okay, I think I've run out of things to say. It's a wrap!
  • P.S. Check out my new site TheManWhoSoldtheWeb.com, where I publish guides and scripts on Internet Marketing and SEO. Here is a limited time freebie: the Rapid Google Indexer.


    » Bookmark this Tutorial
    » Bookmark IceMelon
    Icemelon -- Create Your Own BBCode -- PHP, CSS, Javascript Tutorials, & More!
      © 2005-2010 Icemelon.com   Email: dave[AT]icemelon[D0T]c0m