The Preg and Pattern Functions (part 2)
But what happens if I don't want any names that start with a,b, or c, or even k? What happens if I want the total opposite? You can change the pattern to [l-z], but you can also do this:
What you are looking at is the PREG_GREP_INVERT flag. The flag parameter was added in version 4.2.0 of PHP. Put it in the script, and you will notice how a different set of names appear on the screen. Suppose you have this array:
And you want only the passwords that begin with two numbers (35434 and 34donna).
When you have the lettter d preceded by \, preg_grep considers it a number, any number. Then we have {2,} which means there must be two or more numbers.
Now analyze the following line:
It seems cumbersome. It isn't. Notice the | at the center. It means OR. If you use this pattern, you will echo the passwords that begin with two difits, and you will also get any password that starts with a digit and is followed by one character from k to z (5per).
The preg_match Function
The syntax for this function is as follows:
int preg_match(pattern, subject, matches);
where the pattern will be a regular expression like those of preg_grep and matches is an array created by the function. Using scripts similar to the ones we used before, we can understand how this function works. For starters, we can't use arrays as subjects, which is something we could do with preg_grep. Thus we have no choice but to use a sentence instead of an array as our subject.
if (preg_match('/\d{2,}[a-z]+/', $input, $match)) {
$found_password = $match[0];
echo "The password you seek is: $found_password.";
//The output is: The password you seek is: 34donna.
}
By using \d{2,}[a-z]+ as a pattern, we make it clear we want the first characters to be at least two numbers, and we want the following characters to be letters. Therefore preg_match returns a 1 or a 0 when used, so it can be used inside an if, like it was done in the example.
You probably noticed that 53yurax also matched the given pattern, but it got ignored. This is because preg_match considers only the first match it finds. Something preg_match_all, as we will see later on, does not do.
Using preg_match will turn $match into an array. The first instance of the array ($match[0]) will always be the first part of the string that matched the pattern, regardless of any other match there is afterwards. Makes you wonder what else is in the array, doesn't it? Well, preg_match is truly special, for anything inside the pattern that is in parentheses can also become part of that array. Here is the original pattern: \d{2,}[a-z]+. Let's change it a little by adding parentheses: preg_match('/\d{2,}([a-z]{2})([a-z]{2,})/', $input, $match). Now we have the password 34donna being a perfect match. Examine the script below:
if (preg_match('/\d{2,}([a-z]{2})([a-z]{2,})/', $input, $match)) {
echo "The entire string is $match[0]. Then there is $match[1] and there is $match[2].";
/*The output will be The entire string is 34donna. Then there is do and there is nna. */
}
That part of the string that matches the entire pattern becomes part of $match[0], and whatever is inside parentheses becomes part of the rest of the array.
Let's wrap this up with one more script:
{
echo "<br />My name is $match[0]<br />\n";
}
//My name is Brenda
In this case, \b stands for boundary. Nothing that isn't the name Brenda as it is will be considered. At this point, you should've noticed how useful preg_match is when it comes to extracting patterns from that string. However, there's a problem: this function only considers only the first instance of a pattern, ignoring the rest.
Return to << Page 1 of this tutorial | Go to Page 3 >> of this tutorial.



