Tutorials PHP Regular Expressions

Regular Expressions

I'm going to show you how to match and replace different statements in variables.

What are regular expression?

Regular expressions are patterns that PHP recognizes. Common statements on the web all have the same patterns. URLs always start with http://______.___ or https://______.___ and emails look like ______@____.___ so what would happen if you could search for these patterns?

How do I use regular expressions?

(happy) brackets group characters and saves it to a variable. You can use the variable as $1,$2,$3 depending on the location in a preg_replace.
happy a string without special characters matches the exact statement anywhere in the string. "He's a happy boy" is a match.
^happ a caret matches a string that starts with it. It will match "happiness comes from the heart" but not "a happy employee is a productive employee"
happy$ a dollar sign after a pattern matches a string that ends with it. It will match "money can't make you happy" but not "if you're happy and you know it"
^happy$ matches the beginning and end of a string. The only match is "happy"
[123] square brackets match anything in between. This will match "1" or "2" or "3"
.appy a period matches any character. This will match "happy", "sappy", "nappy", or "gappy"
happy* an astrix means the character before it could appear multiple times. This would match "happ" and also "happyyyy"
happy|sad a pipeline matches either one of the statements before or after. This would match either "happy" or "sad"
happy+ a plus sign matches the character before it multiple times. It would match "happy" and "happyyyyy"
happy? a question mark matches the character before it maximum once. This can only match "happ" and "happy"

Note: You have to use a backslash (\) to escape anything that could be interpreted as part of a pattern. You need to escape "^.<[$()'|*+?{\/ if you want its literal meaning. In order to search for the string Hello?, you have to use Hello\?.

Commonly used expressions

So what are simple basic expressions that are routinely searched for? Here's a small list to start you off.

"/https?:\/\/(www\.)?[a-zA-Z0-9_-]+\.[a-zA-Z]+/i" searches for the typical URL with "http", optional "s", a colon followed by two slashes, optional triple Ws and period, any number of characters, period, any number of letters.

"/[a-zA-Z0-9_-]+@[a-zA-Z]+\.[a-zA-Z\/]+/i" searches for the typical email with any number of characters, the @ symbol, any number of characters, period, any number of letters.

What can I do with expressions?

The two most common things you can do with expressions are matching or replacing strings. You can use preg_match to match strings and preg_replace to replace strings.

$string  = 'http://www.yahoo.com';
$pattern = '/(https?:\/\/)(www\.)?([a-zA-Z0-9_-]+)\.([a-zA-Z\/]+)/i';

if (preg_match($pattern,$string)) {
  echo 'This is a url.';
}

$replacement = "<a href=\"$1$2$3.$4\">$1$2$3.$4</a>";
$replace     = preg_replace($pattern,$replacement,$string);
// Produces "This is a url. <a href="http://www.yahoo.com">http://www.yahoo.com</a>"
echo $replace;

Returning a match

Sometimes you have content and you need to extract a certain part of it. You can use preg_match to return a certain part of a string. Adding a third variable into the preg_match statement will return it with an array of matches.

$string  = '<p><a href="http://www.yahoo.com" target="_blank">Yahoo!</a></p>';
$pattern = '/href=\"(.*?)\"/i';
preg_match($pattern,$string,$array);

// Produces "The URL is: http://www.yahoo.com"
echo 'The URL is: '. $array[1];

Why should I care?

You can use these statements to validate forms. If you're asking for an email or URL, then you can use preg_match to make sure at least it looks like an email or URL. You might also want to replace certain statements. If you have user generated content, you might want to preg_replace to put anchor tags on all URLs or emails.

I can't remember all this!

This might be a little intimidating and hard to learn right now, and even I can't remember this stuff. I have a whole cheat sheet dedicated to regular expressions that I "borrowed" from another tutorial site. If you need to match something, just Google "_______ regular expression" and you should find it quickly. But try it yourself whenever possible, because practice makes perfect.

PS. If you want to replace an exact statement instead of a pattern, str_replace is easier to use.

Posted by on . Category: PHP


Comments

No comments posted yet

You need to register or login to post new comments.