PHP Regular Expressions

This item was filled under [ PHP & MySql, References ]

Regular expressions can be used for matching or replacing certain patters inside a string. A simple example would be to check whether a certain word is inside a certain string and maybe on a certain position ( begin / end of string ). However also more complicated patterns can be made ( for example to verify whether a string is an URL address ).To do this, regular expressions can be used. Functions that make use of regular expressions in PHP are: preg_replace, preg_match and preg_match_all. These are the functions that will be used inside examples with regular expressions.

To match a regular expression pattern with a string, we may use the preg_match or preg_match_all function. The difference between these two functions is that preg_match stops searching for a match after one has occurred while preg_match_all continues to search for all matches. The preg_match function therefore is moreover used to check whether a pattern occurs inside a string, while the preg_match_all function is used to actually retrieve all matches with the pattern inside the string. So how do we set up a pattern? Therefore multiple symbols can be used. Here’s a table of the most common ones:

SymbolMeaning
^The word after this symbol should be at the beginning of the string to match.
$The word before this symbol should be at the end of the string to match.
*The expression before this symbol may occur any amount of times in the string to
match.
?The expression before this symbol may or may not occur once.
+The expression before this symbol should at least occur once.
{x}The expression before this symbol should occur x amount of times.
{x1,x1}The expression before this symbol should occur atleast x1 amount of times,
max x2 amount of times.
.Any character.
|Either the expression before or after this symbol should match.
ExpressionMeaning
bar$The string to match should contain the word ‘bar’ at the end of the string.
^foo(.*)bar$The string to match should contain the word ‘foo’ at the beginning of the
string AND the word ‘bar’ at the end of the string.
^[A-Za-z]*[0-9]*$The string to match should start with only letters and end with only numbers.
.* Any amount of any characters.
[A-Za-z]Any letter ( basicly: all the characters between the brackets are allowed).
[A-Za-z]*Any amount of letters ( but also: only letters ).
[A-Z]|[a-z]Either a capital or a non-capital letter.
^fooThe string to match should contain the word ‘foo’ at the beginning of the
string.

preg_match example


<?php
//preg_match([pattern], [string], [match], [flag], [offset])

$pattern = “/^[A-Za-z0-9_]*@[A-Za-z0-9_]*\.com$/”;
$str1       = “match_example@provider.com”;
$str2       = “no_match_example@provider.nl”;
$str3       = “no-match@[test.com”;
$str4      = “example_match123@456web.com”;

if(preg_match($pattern, $str1)) {
echo “$str1 matches.”;
}else{
echo “$str1 does not match.”;
}

if(preg_match($pattern, $str2)) {
echo “$str2 matches.”;
}else{
echo “$str2 does not match.”;
}

if(preg_match($pattern, $str3)) {
echo “$str3 matches.”;
}else{
echo “$str3 does not match.”;
}

if(preg_match($pattern, $str4)) {
echo “$str4 matches.”;
}else{
echo “$str4 does not match.”;
}
?>

preg_match_all example

<?php
//preg_match_all([pattern], [string], [matches], [flag], [offset])
$pattern = “match[0-9]”;
$str         = “this is match1 and this is match2”;
if(preg_match_all($pattern, $str, $matches)) {
print_r($matches); //prints the array containing all matches found
}
?>
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Comment