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

Regular Expressions
By dave

What are regular expressions? They are essentially string patterns. For a better explanation, check this out [http://en.wikipedia.org/wiki/Regular_expression].

Knowing how to effectively write regular expressions is an extremely useful. You will also feel very powerful. Below are some possible uses:
1) Check if the text "IceMelon" appears in a string.
2) Check that an username contains only letters, numbers, or underscores.
3) Filter out URLs.

Let's talk about Use One. You can do this easily with the following expression:

/IceMelon/

I'll break this down. The expression you are searching for is contained within forward slashes (/). Okay, simple enough. But, what if you wanted the search to be case-insensitive? Add a 'i' to the end:

/icemelon/i

What if you wanted to check if an expression began with 'icemelon?'

/^icemelon/i

The carrot '^' denotes start of a string. What if you wanted to check if something ended with 'icemelon?'

/icemelon$/i

The dollar sign '$' denotes the end of a string. What if you wanted to check if a string contained either the words of 'ice' or 'melon' (in any order)?

/(ice|melon)/i

If you want to present a selection of strings, put them in parenthesis and add |'s between each string.

Now, Let's look at Use Two. Consider the following scenario: You run a weblog service and want to limit usernames to only numbers, letters, and underscores. How do you do that? Check out this expression.

/^\w+$/i

Wtf? Don't worry, I'll break that down.
» The '\w' signifies a "word" character, where a "word" is either a letter, number, or underscore.
» The '+' means 1 or more of the preceding character.

Altogether, the expression is checking that from the start till the end of the string, each character is either a letter or number. The '+' is very important. Without it, you would be looking for a one character string—in other words, a letter or a number. The '^' and '$' are also significant, because without them, a string like '##abcd##' would be acceptable.

Here are some alternatives to the "\w" syntax:
\d — any decimal digit
\D — any character that is not a decimal digit
\s — any whitespace character
\S — any character that is not a whitespace character
\W — any "non-word" character
. — any character (NOTE: if you want to specify the period, you need to backslash it: i.e. '\.')

Here are some alternatives to he '+' syntax:
? — zero or one of the previous character
* — zero or more of the previous character

So, now that you're an expert, how do you put this shit to use? Check out these convenient built-in PHP functions:

preg_match
preg_match_all
preg_replace
preg_split
preg_grep
preg_replace_callback

Then, give Use Three a try yourself. If you haven't convinced yourself yet, regular expressions are really useful. In fact, I all those code block formatting and php.net links on the tutorials were generated using regular expressions. For example..
$tutorial = preg_replace("/URL\[(.+)\]/", "[<a href=\"\$1\" target=_blank>\$1</a>]", $tutorial);

Go figure.

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 -- Regular Expressions -- PHP, CSS, Javascript Tutorials, & More!
  © 2005-2010 Icemelon.com   Email: dave[AT]icemelon[D0T]c0m