Matching

preg_match($s1,$s2[,&$arr[,$i1[,$i2]]]) returns 1 if the there is a match for the regular expression $s1 in $s2. $arr[0] contains the substring that matches the full pattern, $arr[1] contains the substring that matches the first parenthesized subpattern, and so on. If $i1 is set to PREG_OFFSET_CAPTURE, the string offset too will be returned. $i2 specifies the position from which to start the search. preg_match_all($s1,$s2[,&$arr [,$i1=PREG_PATTERN_ORDER[,$i2]]]) is similar to preg_match() except that all matches are stored in $arr. It returns the number of full pattern matches. $i1=PREG_SET_ORDER orders the results so that $arr[0] is an array of the first set of matches, $arr[1] is an array of the second set of matches, and so on.

<!DOCTYPE html><html><head></head>
<body><?php

$s = '--abcXXYY--abcMMNN--';
preg_match('/abc(..)(..)/',$s,$arr);
print_r($arr); echo "<br />";
preg_match('/abc(..)(..)/',$s,$arr,PREG_OFFSET_CAPTURE);
print_r($arr); echo "<br /><br />";

preg_match_all('/abc(..)(..)/',$s,$arr);
print_r($arr); echo "<br />";
preg_match_all('/abc(..)(..)/',$s,$arr,PREG_SET_ORDER);
print_r($arr); echo "<br />";
preg_match_all('/abc(..)(..)/',$s,$arr,PREG_SET_ORDER|PREG_OFFSET_CAPTURE);
print_r($arr); echo "<br />";
?> </body></html>

Array ( [0] => abcXXYY 
            [1] => XX 
            [2] => YY ) 
Array ( [0] => Array ( [0] => abcXXYY [1] => 2 ) 
            [1] => Array ( [0] => XX [1] => 5 ) 
            [2] => Array ( [0] => YY [1] => 7 ) ) 

Array ( [0] => Array ( [0] => abcXXYY [1] => abcMMNN ) 
            [1] => Array ( [0] => XX [1] => MM ) 
            [2] => Array ( [0] => YY [1] => NN ) ) 
Array ( [0] => Array ( [0] => abcXXYY [1] => XX [2] => YY ) 
            [1] => Array ( [0] => abcMMNN [1] => MM [2] => NN ) ) 
Array ( [0] => Array ( [0] => Array ( [0] => abcXXYY [1] => 2 )
                                     [1] => Array ( [0] => XX [1] => 5 ) 
                                     [2] => Array ( [0] => YY [1] => 7 ) ) 
             [1] => Array ( [0] => Array ( [0] => abcMMNN [1] => 11 )
                                     [1] => Array ( [0] => MM [1] => 14 ) 
                                     [2] => Array ( [0] => NN [1] => 16 ) ) )