spanish icon   Spanish version


english icon  English version

News

03-24-2009:PHP Regular Expressions tutorial added to the PHP tutorials section.


03-19-2009: Two old templates (Photoblue and Hardwarezip) added to the webpage.


03-01-2009:Preg and Pattern Functions tutorial added to the PHP tutorials section.


About Redacron:

We are the creators of:


We have worked on the following websites:


Email Icon  Write to Us


Valid XHTML 1.0 Transitional


Links

Managed IT services Managed IT Services can minimise the burden of managing the IT infrastructure. Datashare Solutions Managed IT Services are perfect if your business needs to control costs or ensure a fixed level of IT service performance.

Webmaster Toolkit Collection of webmaster tools developed to help webmasters with our daily webmaster chores.

Geobytes For the web developer, Geobytes content localization technology is 'rain maker' technology.


 



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:

$input = "The passwords are 3bag and 34donna and also 53yurax.";
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:

$input = "The passwords are 3bag and 34donna and also 53yurax.";
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:

$input = "We have these metacharacters: ? $ < > ( ) and finally we have: /.";

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.

$input = "We have these metacharacters: ? $ < > ( ) and finally we have: /."; $result = preg_quote($input); echo $result; /* Output: We have the following\: \? \$ \< \> \( \) and finally we have\: \/\. */

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:

$text = "http://www.google.com";
$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.

Note: it is worth mentioning that when it comes to the replacement's syntax, $1 and \\1 are the same thing.

Return to << Page 2 of this tutorial    |   Go to Page 4 >> of this tutorial.





Home

Services: Joomla/osCommerce | Search Engine Optimization | Logo Design | Web Design

Portfolio: Catalog Design | Logo and Banner Design | Banner and Header Portfolio | Web Design

Tutorials: All Tutorials



Copyright © 2008 Redacron Studios. Design by R.P Carbonell.

Fatal error: Call to undefined function send_numbers() in /var/www/vhosts/redacron.com/httpdocs/includes/bottom_2.php on line 37