What are Regular ... regular ... is a pattern that can match various text strings. Using regular ... you can find (and replace) certain text ... for example "all the wor
What are Regular Expressions?
A regular expression is a pattern that can match various text strings. Using regular expressions you can find (and replace) certain text patterns, for example "all the words that begin with the letter A" or "find only telephone numbers". Regular expressions are often used in validation classes, because they are a really powerful tool to verify e-mail addresses, telephone numbers, street addresses, zip codes, and more.
In this tutorial I will show you how regular expressions work in PHP, and give you a short introduction on writing your own regular expressions. I will also give you several example regular expressions that are often used.
Regular Expressions in PHP
Using regex (regular expressions) is really easy in PHP, and there are several functions that exist to do regex finding and replacing. Let's start with a simple regex find.
Have a look at the documentation of the preg_match function. As you can see from the documentation, preg_match is used to perform a regular expression. In this case no replacing is done, only a simple find. Copy the code below to give it a try.
<?phpAfter having run the code, it's probably a good idea if I do a quick run through the code. Basically, the whole core of the above code is the line that contains the preg_match. The first argument is your regex pattern. This is probably the most important. Later on in this tutorial, I will explain some basic regular expressions, but if you really want to learn regular expression then it's best if you look on Google for specific regular expression examples.
// Example string
$str = "Let's find the stuff <bla>in between</bla> these two previous brackets";
// Let's perform the regex
$do = preg_match("/<bla>(.*)</bla>/", $str, $matches);
// Check if regex was successful
if ($do = true) {
// Matched something, show the matched string
echo htmlentities($matches['0']);
// Also how the text in between the tags
echo '<br />' . $matches['1'];
} else {
// No Match
echo "Couldn't find a match";
}
?>
<?phpThe result would then be the same string, except it would now say 'new stuff' between the bla tags. This is of course just a simple example, and more advanced replacements can be done.
// Example string
$str = "Let's replace the <bla>stuff between</bla> the bla brackets";
// Do the preg replace
$result = preg_replace ("/<bla>(.*)</bla>/", "<bla>new stuff</bla>", $str);
echo htmlentities($result);
?>
<?phpThis would then print "Let's replace the
// Example string
$str = "Let's replace the <bla>stuff between</bla> the bla brackets";
// Do the preg replace
$result = preg_replace ("/<bla>(.*)</bla>/", "<bla>new stuff (the old: $1)</bla>", $str);
echo htmlentities($result);
?>
<?phpThe result of this would be "Valid E-mail. Invalid E-mail", of course. We have just checked if an e-mail address is valid. If you wrap the above code in a function, you've got yourself a e-mail validation function. Keep in mind though that the regex isn't perfect: after all, it doesn't check whether the extension is too long, does it? Because I want to keep this tutorial short, I won't give the full fledged regex, but you can find it easily via Google.
// Good e-mail
$good = "john@example.com";
// Bad e-mail
$bad = "blabla@blabla";
// Let's check the good e-mail
if (preg_match("/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$/", $good)) {
echo "Valid e-mail";
} else {
echo "Invalid e-mail";
}
echo '<br />';
// And check the bad e-mail
if (preg_match("/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$/", $bad)) {
echo "Valid e-mail";
} else {
echo "Invalid e-mail";
}
?>
<?phpThe regex is fairly simple, because we use d. This basically means "match any digit" with the length behind it. In this example it first looks for 3 digits, then a '-' (hyphen) and finally 7 digits. Works perfectly, and does exactly what we want.
// Good number
$good = "123-4567890";
// Bad number
$bad = "45-3423423";
// Let's check the good number
if (preg_match("/d{3}-d{7}/", $good)) {
echo "Valid number";
} else {
echo "Invalid number";
}
echo '<br />';
// And check the bad number
if (preg_match("/d{3}-d{7}/", $bad)) {
echo "Valid number";
} else {
echo "Invalid number";
}
?>
PHP and Cookies; a good mix!
... have long been used in PHP scripts, and are a very useful ... But what exactly are cookies? Maybe you have used then, but you still don't know exactly what they are. Or you aScreen scraping your way into RSS
... is one the hottest ... at the moment, and even big web ... (such as the New York Times) are getting into RSS as well. However, there are still a lot of websites that doPHP On-The-Fly!
... can be used for a lot of ... things, and is one of the most powerful ... ... ... on the web. Not to mention it's ... cheap and widely used. However, one t