xref: /PHP-7.4/ext/pcre/tests/preg_grep_basic.phpt (revision d7a3edd4)
1--TEST--
2Test preg_grep() function : basic functionality
3--FILE--
4<?php
5/*
6* proto array preg_grep(string regex, array input [, int flags])
7* Function is implemented in ext/pcre/php_pcre.c
8*/
9$array = array('HTTP://WWW.EXAMPLE.COM', '/index.html', '/info/stat/', 'http://test.uk.com/index/html', '/display/dept.php');
10var_dump($array);
11var_dump(preg_grep('@^HTTP(.*?)\w{2,}$@i', $array)); //finds a string starting with http (regardless of case) (matches two)
12var_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)
13var_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)
14var_dump(preg_grep('@.*?\.co\.uk$@i', $array)); //finds any address ending in .co.uk (matches none)
15var_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)
16
17?>
18--EXPECT--
19array(5) {
20  [0]=>
21  string(22) "HTTP://WWW.EXAMPLE.COM"
22  [1]=>
23  string(11) "/index.html"
24  [2]=>
25  string(11) "/info/stat/"
26  [3]=>
27  string(29) "http://test.uk.com/index/html"
28  [4]=>
29  string(17) "/display/dept.php"
30}
31array(2) {
32  [0]=>
33  string(22) "HTTP://WWW.EXAMPLE.COM"
34  [3]=>
35  string(29) "http://test.uk.com/index/html"
36}
37array(5) {
38  [0]=>
39  string(22) "HTTP://WWW.EXAMPLE.COM"
40  [1]=>
41  string(11) "/index.html"
42  [2]=>
43  string(11) "/info/stat/"
44  [3]=>
45  string(29) "http://test.uk.com/index/html"
46  [4]=>
47  string(17) "/display/dept.php"
48}
49array(1) {
50  [3]=>
51  string(29) "http://test.uk.com/index/html"
52}
53array(0) {
54}
55array(3) {
56  [1]=>
57  string(11) "/index.html"
58  [2]=>
59  string(11) "/info/stat/"
60  [4]=>
61  string(17) "/display/dept.php"
62}
63