| 1 |
lars |
1 |
--TEST--
|
|
|
2 |
Function -- str_ireplace
|
|
|
3 |
--SKIPIF--
|
|
|
4 |
<?php if (function_exists('str_ireplace')) { echo 'skip'; } ?>
|
|
|
5 |
--FILE--
|
|
|
6 |
<?php
|
|
|
7 |
require_once 'PHP/Compat.php';
|
|
|
8 |
PHP_Compat::loadFunction('str_ireplace');
|
|
|
9 |
|
|
|
10 |
//
|
|
|
11 |
// Simple
|
|
|
12 |
//
|
|
|
13 |
|
|
|
14 |
$search = '{object}';
|
|
|
15 |
$replace = 'fence';
|
|
|
16 |
$subject = 'The dog jumped over the {object}';
|
|
|
17 |
|
|
|
18 |
echo str_ireplace($search, $replace, $subject), "\n";
|
|
|
19 |
|
|
|
20 |
//
|
|
|
21 |
// Test 1: With subject as array
|
|
|
22 |
//
|
|
|
23 |
|
|
|
24 |
// As a full array
|
|
|
25 |
$search = '{SUBJECT}';
|
|
|
26 |
$replace = 'Lady';
|
|
|
27 |
$subject = array('A {subject}', 'The {subject}', 'My {subject}');
|
|
|
28 |
print_r(str_ireplace($search, $replace, $subject));
|
|
|
29 |
|
|
|
30 |
// As a single array
|
|
|
31 |
$search = '{SUBJECT}';
|
|
|
32 |
$replace = 'Lady';
|
|
|
33 |
$subject = array('The dog jumped over the {object}');
|
|
|
34 |
print_r(str_ireplace($search, $replace, $subject));
|
|
|
35 |
|
|
|
36 |
|
|
|
37 |
//
|
|
|
38 |
// Test 2: Search as string, replace as array
|
|
|
39 |
//
|
|
|
40 |
|
|
|
41 |
$search = '{object}';
|
|
|
42 |
$replace = array('cat', 'dog', 'tiger');
|
|
|
43 |
$subject = 'The dog jumped over the {object}';
|
|
|
44 |
// Supress the error, no way of knowing how it'll turn out on the users machine
|
|
|
45 |
echo @str_ireplace($search, $replace, $subject), "\n";
|
|
|
46 |
|
|
|
47 |
|
|
|
48 |
//
|
|
|
49 |
// Test 3: Search as array, Replace as string
|
|
|
50 |
//
|
|
|
51 |
|
|
|
52 |
$search = array('{ANIMAL}', '{OBJECT}', '{THING}');
|
|
|
53 |
$replace = 'frog';
|
|
|
54 |
$subject = 'The {animal} jumped over the {object} and the {thing}...';
|
|
|
55 |
echo str_ireplace($search, $replace, $subject), "\n";
|
|
|
56 |
|
|
|
57 |
|
|
|
58 |
//
|
|
|
59 |
// Test 4: Search and Replace as arrays
|
|
|
60 |
//
|
|
|
61 |
|
|
|
62 |
// Simple
|
|
|
63 |
$search = array('{ANIMAL}', '{OBJECT}');
|
|
|
64 |
$replace = array('frog', 'gate');
|
|
|
65 |
$subject = 'The {animal} jumped over the {object}';
|
|
|
66 |
echo str_ireplace($search, $replace, $subject), "\n";
|
|
|
67 |
|
|
|
68 |
// More in search
|
|
|
69 |
$search = array('{ANIMAL}', '{OBJECT}', '{THING}');
|
|
|
70 |
$replace = array('frog', 'gate');
|
|
|
71 |
$subject = 'The {animal} jumped over the {object} and the {thing}...';
|
|
|
72 |
echo str_ireplace($search, $replace, $subject), "\n";
|
|
|
73 |
|
|
|
74 |
// More in replace
|
|
|
75 |
$search = array('{ANIMAL}', '{OBJECT}');
|
|
|
76 |
$replace = array('frog', 'gate', 'door');
|
|
|
77 |
$subject = 'The {animal} jumped over the {object} and the {thing}...';
|
|
|
78 |
echo str_ireplace($search, $replace, $subject), "\n";
|
|
|
79 |
|
|
|
80 |
|
|
|
81 |
//
|
|
|
82 |
// Test 5: All arrays
|
|
|
83 |
//
|
|
|
84 |
|
|
|
85 |
$search = array('{ANIMAL}', '{OBJECT}', '{THING}');
|
|
|
86 |
$replace = array('frog', 'gate', 'beer');
|
|
|
87 |
$subject = array('A {animal}', 'The {object}', 'My {thing}');
|
|
|
88 |
print_r(str_ireplace($search, $replace, $subject));
|
|
|
89 |
|
|
|
90 |
?>
|
|
|
91 |
--EXPECT--
|
|
|
92 |
The dog jumped over the fence
|
|
|
93 |
Array
|
|
|
94 |
(
|
|
|
95 |
[0] => A Lady
|
|
|
96 |
[1] => The Lady
|
|
|
97 |
[2] => My Lady
|
|
|
98 |
)
|
|
|
99 |
Array
|
|
|
100 |
(
|
|
|
101 |
[0] => The dog jumped over the {object}
|
|
|
102 |
)
|
|
|
103 |
The dog jumped over the Array
|
|
|
104 |
The frog jumped over the frog and the frog...
|
|
|
105 |
The frog jumped over the gate
|
|
|
106 |
The frog jumped over the gate and the ...
|
|
|
107 |
The frog jumped over the gate and the {thing}...
|
|
|
108 |
Array
|
|
|
109 |
(
|
|
|
110 |
[0] => A frog
|
|
|
111 |
[1] => The gate
|
|
|
112 |
[2] => My beer
|
|
|
113 |
)
|