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