1--TEST--
2Test mb_ereg() function : usage variations - match special characters
3--EXTENSIONS--
4mbstring
5--SKIPIF--
6<?php
7function_exists('mb_ereg') or die("skip mb_ereg() is not available in this build");
8?>
9--FILE--
10<?php
11/*
12 * Test how mb_ereg() matches special characters for $pattern
13 */
14
15echo "*** Testing mb_ereg() : usage variations ***\n";
16
17if(mb_regex_encoding('utf-8') == true) {
18    echo "Regex encoding set to utf-8\n";
19} else {
20    echo "Could not set regex encoding to utf-8\n";
21}
22
23$regex_char = array ('\w+' => '\w+',
24                     '\W+' => '\W+',
25                     '\s+' => '\s+',
26                     '\S+' => '\S+',
27                     '\d+' => '\d+',
28                     '\D+' => '\D+',
29                     '\b' =>  '\b',
30                     '\B' =>  '\B');
31
32$string_ascii = 'This is an English string. 0123456789.';
33$string_mb = base64_decode('5pel5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMDEyMzTvvJXvvJbvvJfvvJjvvJnjgII=');
34
35foreach ($regex_char as $displayChar => $char) {
36    echo "\n--** Pattern is: $displayChar **--\n";
37    if (@$regs_ascii || @$regs_mb) {
38        $regs_ascii = null;
39        $regs_mb = null;
40    }
41    echo "-- ASCII String: --\n";
42    var_dump(mb_ereg($char, $string_ascii, $regs_ascii));
43    base64_encode_var_dump($regs_ascii);
44
45    echo "-- Multibyte String: --\n";
46    var_dump(mb_ereg($char, $string_mb, $regs_mb));
47    base64_encode_var_dump($regs_mb);
48
49}
50
51/**
52 * replicate a var dump of an array but outputted string values are base64 encoded
53 *
54 * @param array $regs
55 */
56function base64_encode_var_dump($regs) {
57    if ($regs) {
58        echo "array(" . count($regs) . ") {\n";
59        foreach ($regs as $key => $value) {
60            echo "  [$key]=>\n  ";
61            if (is_string($value)) {
62                var_dump(base64_encode($value));
63            } else {
64                var_dump($value);
65            }
66        }
67        echo "}\n";
68    } else {
69        echo "NULL\n";
70    }
71}
72
73echo "Done";
74
75?>
76--EXPECT--
77*** Testing mb_ereg() : usage variations ***
78Regex encoding set to utf-8
79
80--** Pattern is: \w+ **--
81-- ASCII String: --
82bool(true)
83array(1) {
84  [0]=>
85  string(8) "VGhpcw=="
86}
87-- Multibyte String: --
88bool(true)
89array(1) {
90  [0]=>
91  string(36) "5pel5pys6Kqe44OG44Kt44K544OI44Gn44GZ"
92}
93
94--** Pattern is: \W+ **--
95-- ASCII String: --
96bool(true)
97array(1) {
98  [0]=>
99  string(4) "IA=="
100}
101-- Multibyte String: --
102bool(true)
103array(1) {
104  [0]=>
105  string(4) "44CC"
106}
107
108--** Pattern is: \s+ **--
109-- ASCII String: --
110bool(true)
111array(1) {
112  [0]=>
113  string(4) "IA=="
114}
115-- Multibyte String: --
116bool(false)
117NULL
118
119--** Pattern is: \S+ **--
120-- ASCII String: --
121bool(true)
122array(1) {
123  [0]=>
124  string(8) "VGhpcw=="
125}
126-- Multibyte String: --
127bool(true)
128array(1) {
129  [0]=>
130  string(72) "5pel5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMDEyMzTvvJXvvJbvvJfvvJjvvJnjgII="
131}
132
133--** Pattern is: \d+ **--
134-- ASCII String: --
135bool(true)
136array(1) {
137  [0]=>
138  string(16) "MDEyMzQ1Njc4OQ=="
139}
140-- Multibyte String: --
141bool(true)
142array(1) {
143  [0]=>
144  string(28) "MDEyMzTvvJXvvJbvvJfvvJjvvJk="
145}
146
147--** Pattern is: \D+ **--
148-- ASCII String: --
149bool(true)
150array(1) {
151  [0]=>
152  string(36) "VGhpcyBpcyBhbiBFbmdsaXNoIHN0cmluZy4g"
153}
154-- Multibyte String: --
155bool(true)
156array(1) {
157  [0]=>
158  string(40) "5pel5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CC"
159}
160
161--** Pattern is: \b **--
162-- ASCII String: --
163bool(true)
164array(1) {
165  [0]=>
166  bool(false)
167}
168-- Multibyte String: --
169bool(true)
170array(1) {
171  [0]=>
172  bool(false)
173}
174
175--** Pattern is: \B **--
176-- ASCII String: --
177bool(true)
178array(1) {
179  [0]=>
180  bool(false)
181}
182-- Multibyte String: --
183bool(true)
184array(1) {
185  [0]=>
186  bool(false)
187}
188Done
189