1--TEST-- 2Test preg_match_all() function : basic functionality 3--FILE-- 4<?php 5/* Prototype : proto int preg_match_all(string pattern, string subject, array subpatterns [, int flags [, int offset]]) 6 * Description: Perform a Perl-style global regular expression match 7 * Source code: ext/pcre/php_pcre.c 8 * Alias to functions: 9*/ 10 11$string = 'Hello, world! This is a test. This is another test. \[4]. 34534 string.'; 12 13var_dump(preg_match_all('/[0-35-9]/', $string, $match1, PREG_OFFSET_CAPTURE|PREG_PATTERN_ORDER, -10)); //finds any digit that's not 4 10 digits from the end(1 match) 14var_dump($match1); 15 16var_dump(preg_match_all('/[tT]his is a(.*?)\./', $string, $match2, PREG_SET_ORDER)); //finds "This is a test." and "This is another test." (non-greedy) (2 matches) 17var_dump($match2); 18 19var_dump(preg_match_all('@\. \\\(.*).@', $string, $match3, PREG_PATTERN_ORDER)); //finds ".\ [...]" and everything else to the end of the string. (greedy) (1 match) 20var_dump($match3); 21 22var_dump(preg_match_all('/\d{2}$/', $string, $match4)); //tries to find 2 digits at the end of a string (0 matches) 23var_dump($match4); 24 25var_dump(preg_match_all('/(This is a ){2}(.*)\stest/', $string, $match5)); //tries to find "This is aThis is a [...] test" (0 matches) 26var_dump($match5); 27?> 28--EXPECTF-- 29int(1) 30array(1) { 31 [0]=> 32 array(1) { 33 [0]=> 34 array(2) { 35 [0]=> 36 string(1) "3" 37 [1]=> 38 int(61) 39 } 40 } 41} 42int(2) 43array(2) { 44 [0]=> 45 array(2) { 46 [0]=> 47 string(15) "This is a test." 48 [1]=> 49 string(5) " test" 50 } 51 [1]=> 52 array(2) { 53 [0]=> 54 string(21) "This is another test." 55 [1]=> 56 string(11) "nother test" 57 } 58} 59int(1) 60array(2) { 61 [0]=> 62 array(1) { 63 [0]=> 64 string(21) ". \[4]. 34534 string." 65 } 66 [1]=> 67 array(1) { 68 [0]=> 69 string(17) "[4]. 34534 string" 70 } 71} 72int(0) 73array(1) { 74 [0]=> 75 array(0) { 76 } 77} 78int(0) 79array(3) { 80 [0]=> 81 array(0) { 82 } 83 [1]=> 84 array(0) { 85 } 86 [2]=> 87 array(0) { 88 } 89} 90