Thursday, June 28, 2012

Introduction to Preg in PHP

The PHP function, preg_grep, is used to search an array for specific patterns and then return a new array based on that filtering. There are two ways to return the results. You can return them as is, or you can invert them (instead of only returning what matches, it would only return what does not match.) It is phrased as: preg_grep ( search_pattern, $your_array, optional_inverse ) The search_pattern needs to be a regular expression. If you are unfamiliar with them This Article gives you an overview of the syntax.
 <? 
 $data = array(0, 1, 2, 'three', 4, 5, 'six', 7, 8, 'nine', 10); 
 $mod1 = preg_grep("/4|5|6/", $data); 
 $mod2 = preg_grep("/[0-9]/", $data, PREG_GREP_INVERT); 
 print_r($mod1); 
 echo "<br>"; 
 print_r($mod2); 
 ?> 
This code would result in the following data:
Array ( [4] => 4 [5] => 5 ) 
Array ( [3] => three [6] => six [9] => nine )



First we assign our $data variable. This is a list of numbers, some in alpha form, others in numeric. The first thing we run is called $mod1. Here we are searching for anything that contains 4, 5, or 6. When our result is printed below we only get 4 and 5, because 6 was written as 'six' so it did not match our search.
Next we run $mod2, which is searching for anything that contains a numeric character. But this time we include PREG_GREP_INVERT. This will invert our data, so instead of outputting numbers, it outputs all of our entries that where not numeric (three, six and nine).
Preg_Match : 
The Preg_Match PHP function is used to search a string, and return a 1 or 0. If the search was successful a 1 will be returned, and if it was not found a 0 will be returned. Although other variables can be added, it is most simply phrased as: preg_match(search_pattern, your_string). The search_pattern needs to be a regular expression.

 <? 
 $data = "I had a box of cerial for breakfast today, and then I drank some juice.";
 if (preg_match("/juice/", $data))
 {
 echo "You had juice.<br>";
 }
 else
 {
 echo "You had did not have juice.<br>";
 }
 if (preg_match("/eggs/", $data))
 {
 echo "You had eggs.<br>";
 }
 else
 {
 echo "You had did not have eggs.<br>";
 } 
 ?>
The code above uses preg_match to check for a key word (first juice then egg) and replies based on whether it is true (1) or false (0). Because it returns these two values it is most often used in a conditional statement
Preg_Match_All :
Preg_Match_All is used to search a string for specific patterns and stores the results in an array. Unlike preg_match which stops searching after it finds a match, preg_match_all searches the entire string and records all matches. It is phrased as: preg_match_all (pattern, string, $array, optional_ordering, optional_offset)
 <? $data = "The party will start at 10:30 pm and run untill 12:30 am"; 
 preg_match_all('/(\d+:\d+)\s*(am|pm)/', $data, $match, PREG_PATTERN_ORDER);
 echo "Full: <br>"; 
 print_r($match[0]); 
 echo "<p>Raw: <br>"; 
 print_r($match[1]); 
 echo "<p>Tags: <br>"; 
 print_r($match[2]); 
 ?> 
In our first example we use PREG_PATTERN_ORDER. We are searching for 2 things; one is the time, the other is it's am/pm tag. Our results are outputted to $match, as an array where $match[0] contains all matches, $match[1] contains all data matching our first sub-serach (the time) and $match[2] contains all data matching our second sub-search (am/pm).
 <? $data = "The party will start at 10:30 pm and run untill 12:30 am"; 
 preg_match_all('/(\d+:\d+)\s*(am|pm)/', $data, $match, PREG_SET_ORDER); 
 echo "First: <br>"; 
 echo $match[0][0] . " , " . $match[0][1] . " , ". $match [0][2] ."<br>"; 
 echo "Second: <br>"; 
 echo $match[1][0] . " , " . $match[1][1] . " , ". $match [1][2] ."<br>";
 ?> 
In our second example we use PREG_SET_ORDER. This puts each full result into an array. The first result is $match[0], with $match[0][0] being the full match, $match[0][1] being the first sub-match and $match[0][2] being the second sub-match.

Preg_replace :
The preg_replace function is used to do a find-and-replace on a string or an array. We can give it one thing to find and replace (for example it seeks out the word 'him' and changes it to 'her') or we can give it a full list of things (an array) to search for, each with a corresponding replacement. It is phrased as preg_replace ( search_for, replace_with, your_data , optional_limit, optional_count ) The limit will default to -1 which is no limit. Remember your_data can be a string or an array.
<? 
 $data = "The cat likes to sit on the fence. He also likes to climb the tree."; 
 
 $find ="/the/"; 
 $replace ="a"; 
 
 //1. replace single word 
 Echo "$data <br>"; 
 Echo preg_replace ($find, $replace, $data); 
 
 //create arrays 
 $find2 = array ('/the/', '/cat/'); 
 $replace2 = array ('a', 'dog'); 
 
 //2. replace with array values 
 Echo preg_replace ($find2, $replace2, $data); 
 
 //3. Replace just once
 Echo preg_replace ($find2, $replace2, $data, 1); 
 
 //4. Keep a count of replacements 
 $count = 0; 
 Echo preg_replace ($find2, $replace2, $data, -1, $count); 
 Echo "<br>You have made $count replacements"; 
 
 ?> 
In our first example we simply replace 'the' with 'a'. As you can see these are cAse seNsiTIvE. Then we set up an array, so in our second example we are replacing both the words 'the' and 'cat'. In our third example, we set the limit to 1, so each word is only replaced one time. Finally in our 4th example, we keep count of how many replacements we have made.
Preg_Spilit :
The function Preg_Spilit is used to take a string, and put it into an array. The string is broken up into different values in the array based upon your input. It is phrased as preg_split ( split_pattern, your_data, optional_limit, optional_flags )
 <?php 
 $str = 'I like goats. You like cats. He likes dogs.'; 
 $chars = preg_split('//', $str); 
 print_r($chars); 
 echo "<p>"; 
 $words= preg_split('/ /', $str); 
 print_r($words); 
 echo "<p>"; 
 $sentances= preg_split('/\./', $str, -1 , PREG_SPLIT_NO_EMPTY); 
 print_r($sentances); 
 ?> 
In the code above we preform three splits. In our first, we split the data by each character. In the second, we split it with a blank space, thus giving each word (and not each letter) an array entry. And in our third example we use a '.' period to split the data, therefor giving each sentence it's own array entry.
Because in our last example we use a '.' period to split, a new entry is started after our final period, so we add the flag PREG_SPLIT_NO_EMPTY so that no empty results are returned. Other available flags are PREG_SPLIT_DELIM_CAPTURE which also captures the character you are splitting by (our "." for example) and PREG_SPLIT_OFFSET_CAPTURE which captures the offset in characters where the split has occurred.




No comments:

Post a Comment