]> granicus.if.org Git - php/blob
2401e0bb49
[php] /
1 --TEST--
2 Test preg_replace() function : error conditions - wrong arg types
3 --FILE--
4 <?php
5 /*
6 * proto string preg_replace(mixed regex, mixed replace, mixed subject [, int limit [, count]])
7 * Function is implemented in ext/pcre/php_pcre.c
8 */
9 /*
10 * Testing how preg_replace reacts to being passed the wrong type of replacement argument
11 */
12 echo "*** Testing preg_replace() : error conditions ***\n";
13 $regex = '/[a-zA-Z]/';
14 $replace = array('this is a string', array('this is', 'a subarray'),);
15 $subject = 'test';
16 foreach($replace as $value) {
17     @print "\nArg value is: $value\n";
18     try {
19         var_dump(preg_replace($regex, $value, $subject));
20     } catch (TypeError $e) {
21         echo $e->getMessage(), "\n";
22     }
23 }
24 $value = new stdclass(); //Object
25 try {
26     var_dump(preg_replace($regex, $value, $subject));
27 } catch (Error $e) {
28     echo $e->getMessage(), "\n";
29 }
30 echo "Done";
31 ?>
32 --EXPECTF--
33 *** Testing preg_replace() : error conditions ***
34
35 Arg value is: this is a string
36 string(64) "this is a stringthis is a stringthis is a stringthis is a string"
37
38 Arg value is: Array
39 preg_replace(): Argument #1 ($regex) must be of type array when argument #2 ($replace) is an array, string given
40 Object of class stdClass could not be converted to string
41 Done