xref: /PHP-8.0/ext/pcre/tests/preg_grep_basic.phpt (revision b5c7a83d)
1--TEST--
2Test preg_grep() function : basic functionality
3--FILE--
4<?php
5/*
6* Function is implemented in ext/pcre/php_pcre.c
7*/
8$array = array('HTTP://WWW.EXAMPLE.COM', '/index.html', '/info/stat/', 'http://test.uk.com/index/html', '/display/dept.php');
9var_dump($array);
10var_dump(preg_grep('@^HTTP(.*?)\w{2,}$@i', $array)); //finds a string starting with http (regardless of case) (matches two)
11var_dump(preg_grep('@(/\w+\.*/*)+@', $array)); //finds / followed by one or more of a-z, A-Z and 0-9, followed by zero or more . followed by zero or more / all more than once. (matches all)
12var_dump(preg_grep('@^http://[^w]{3}.*$@i', $array)); //finds http:// (at the beginning of a string) not followed by 3 characters that aren't w's then anything to the end of the sttring (matches one)
13var_dump(preg_grep('@.*?\.co\.uk$@i', $array)); //finds any address ending in .co.uk (matches none)
14var_dump(preg_grep('@^HTTP(.*?)\w{2,}$@i', $array, PREG_GREP_INVERT)); //same as first example but the array created contains everything that is NOT matched but the regex (matches three)
15
16?>
17--EXPECT--
18array(5) {
19  [0]=>
20  string(22) "HTTP://WWW.EXAMPLE.COM"
21  [1]=>
22  string(11) "/index.html"
23  [2]=>
24  string(11) "/info/stat/"
25  [3]=>
26  string(29) "http://test.uk.com/index/html"
27  [4]=>
28  string(17) "/display/dept.php"
29}
30array(2) {
31  [0]=>
32  string(22) "HTTP://WWW.EXAMPLE.COM"
33  [3]=>
34  string(29) "http://test.uk.com/index/html"
35}
36array(5) {
37  [0]=>
38  string(22) "HTTP://WWW.EXAMPLE.COM"
39  [1]=>
40  string(11) "/index.html"
41  [2]=>
42  string(11) "/info/stat/"
43  [3]=>
44  string(29) "http://test.uk.com/index/html"
45  [4]=>
46  string(17) "/display/dept.php"
47}
48array(1) {
49  [3]=>
50  string(29) "http://test.uk.com/index/html"
51}
52array(0) {
53}
54array(3) {
55  [1]=>
56  string(11) "/index.html"
57  [2]=>
58  string(11) "/info/stat/"
59  [4]=>
60  string(17) "/display/dept.php"
61}
62