The Preg and Pattern Functions (part 3)
The preg_match_all Function.
The script below has a variable called $input which contains the same string as the script we analyze before. This time, however, it will become the subject of a different function:
if (preg_match_all('/\d{2,}([a-z]{2})([a-z]{2,})/', $input, $match)) {
echo "For the first password, we have: ".$match[0][0].". Then there is ".$match[1][0]." and there is ".$match[2][0].".<br />\n";
echo "For the second password, we have: ".$match[0][1].". Then there is ".$match[1][1]." and there is ".$match[2][1].".<br />\n"; }
/* The output will be:
For the first password, we have: 34donna. Then there is do and there is nna.
For the second password, we have: 53yurax. Then there is yu and there is rax.
*/
Once again, we have three passwords, but only two match the given pattern. By also considering 5yurax, preg_match_all lives up to its name by creating a multi-dimensional array where $match[x][0] (x can be any number) are values related to the first match, and $match[x][1] are values related to the second match. Just like the previous functions, preg_match_all has flags. When no flag is given, PREG_PATTERN_ORDER is used by default. Thus the second part of the multidimensional array will correspond to the entire matched pattern. For example, the zero in $match[1][0] tells us this value is associated with 34donna, for this is the first part of the string to start with two numbers followed by letters. Thus the 0 is part of the pattern. This is what the flag PREG_PATTERN_ORDER does, ordering the array values by pattern.
You want to order them by the set of values inside parentheses? PREG_SET_ORDER does exactly that. Compare the script below with the previous one:
if (preg_match_all('/\d{2,}([a-z]{2})([a-z]{2,})/', $input, $match, PREG_SET_ORDER)) {
$found_password = $match[0][1];
echo "For the first password, we have: ".$match[0][0].". Then there is ".$match[0][1]." and there is ".$match[0][2].".<br />\n";
echo "For the second password, we have: ".$match[1][0].". Then there is ".$match[1][1]." and there is ".$match[1][2].".<br />\n";
}
Notice anything different? Now $match[0][x] is related to the first matched patter, not $match[x][0]. The output will be the same as before, but the multidimensional arrays are different.
The preg_quote Function.
Let's have this as our new $input:
Perhaps you will never stumbled into a string that contains that kind of data, but what if you do? Finding out what characters are metacharacters like ? $ > < ( ) may be time consuming, but if you do not put a \ before those characters, a function like preg_grep can come out with an error. That's what makes preg_quote useful.
Now you can use $result as a pattern for a function like preg_grep without having any regex character affecting the outcome.
The preg_replace Function.
preg_replace(pattern, replacement, subject, limit(optional));
This function searches for the pattern in the subject's string and replaces it. Using a limit is optional. If the limit is omitted or is -1, all the matches are replaced. The script below is a fine example of how useful this function is:
$pattern = "#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is";
$replacement = "\\1<a href=\"\\2\" target=\"_blank\">Click here</a>";
$text = preg_replace($pattern, $replacement, $text);
echo "<br />$text";
//The output will be a link to Google with the words Click here.
Remember how the function preg_match returned everything inside parentheses as part of an arra? With preg_replace patterns, you can do something similar. Examine the replacement string on the script above, and you will notice there is a \\1 and a \\2. In this case, \\1 represents the first pattern enclosed in parentheses, and \\2 represents the second one. Numbers preceded by \\ can go from 0 to 99, where 0 is the match for the entire pattern. If there are no matches for the pattern, preg_replace won't alter the subject's string. If subject is an array, preg_replace will alter every component of that array and return an array. If the pattern is n array, and the replacement is a string, preg_replace will go through this pattern-array and replace any match with the given replacement.
Return to << Page 2 of this tutorial | Go to Page 4 >> of this tutorial.



