-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring-functions.php
200 lines (159 loc) · 5.29 KB
/
string-functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/**
* Replace $limit occurences of the search string with the replacement string
* @param mixed $search The value being searched for, otherwise known as the
* needle. An array may be used to designate multiple needles.
* @param mixed $replace The replacement value that replaces found search
* values. An array may be used to designate multiple replacements.
* @param mixed $subject The string or array being searched and replaced on,
* otherwise known as the haystack. If subject is an array, then the search and
* replace is performed with every entry of subject, and the return value is an
* array as well.
* @param string $count If passed, this will be set to the number of
* replacements performed.
* @param int $limit The maximum possible replacements for each pattern in each
* subject string. Defaults to -1 (no limit).
* @return string This function returns a string with the replaced values.
*/
function str_replace_limit(
$search,
$replace,
$subject,
&$count,
$limit = -1
){
// Set some defaults
$count = 0;
// Invalid $limit provided. Throw a warning.
if (!valid_integer($limit)) {
$backtrace = debug_backtrace();
trigger_error('Invalid $limit `'.$limit.'` provided to '.__function__.'() '.
'in `'.$backtrace[0]['file'].'` on line '.$backtrace[0]['line'].'. '.
'Expecting an integer', E_USER_WARNING);
return $subject;
}
// Invalid $limit provided. Throw a warning.
if ($limit<-1) {
$backtrace = debug_backtrace();
trigger_error('Invalid $limit `'.$limit.'` provided to '.__function__.'() '.
'in `'.$backtrace[0]['file'].'` on line '.$backtrace[0]['line'].'. '.
'Expecting -1 or a positive integer', E_USER_WARNING);
return $subject;
}
// No replacements necessary. Throw a notice as this was most likely not the
// intended use. And, if it was (e.g. part of a loop, setting $limit
// dynamically), it can be worked around by simply checking to see if
// $limit === 0, and if it does, skip the function call (and set $count to 0,
// if applicable).
if ($limit === 0) {
$backtrace = debug_backtrace();
trigger_error('Invalid $limit `'.$limit.'` provided to '.__function__.'() '.
'in `'.$backtrace[0]['file'].'` on line '.$backtrace[0]['line'].'. '.
'Expecting -1 or a positive integer', E_USER_NOTICE);
return $subject;
}
// Use str_replace() whenever possible (for performance reasons)
if ($limit === -1) {
return str_replace($search, $replace, $subject, $count);
}
if (is_array($subject)) {
// Loop through $subject values and call this function for each one.
foreach ($subject as $key => $this_subject) {
// Skip values that are arrays (to match str_replace()).
if (!is_array($this_subject)) {
// Call this function again for
$this_function = __FUNCTION__;
$subject[$key] = $this_function(
$search,
$replace,
$this_subject,
$this_count,
$limit
);
// Adjust $count
$count += $this_count;
// Adjust $limit, if not -1
if ($limit != -1) {
$limit -= $this_count;
}
// Reached $limit, return $subject
if ($limit === 0) {
return $subject;
}
}
}
return $subject;
} elseif (is_array($search)) {
// Only treat $replace as an array if $search is also an array (to match
// str_replace())
// Clear keys of $search (to match str_replace()).
$search = array_values($search);
// Clear keys of $replace, if applicable (to match str_replace()).
if (is_array($replace)) {
$replace = array_values($replace);
}
// Loop through $search array.
foreach ($search as $key => $this_search) {
// Don't support multi-dimensional arrays (to match str_replace()).
$this_search = strval($this_search);
// If $replace is an array, use the value of $replace[$key] as the
// replacement. If $replace[$key] doesn't exist, just an empty string (to
// match str_replace()).
if (is_array($replace)) {
if (array_key_exists($key, $replace)) {
$this_replace = strval($replace[$key]);
} else {
$this_replace = '';
}
} else {
$this_replace = strval($replace);
}
// Call this function again for
$this_function = __FUNCTION__;
$subject = $this_function(
$this_search,
$this_replace,
$subject,
$this_count,
$limit
);
// Adjust $count
$count += $this_count;
// Adjust $limit, if not -1
if ($limit != -1) {
$limit -= $this_count;
}
// Reached $limit, return $subject
if ($limit === 0) {
return $subject;
}
}
return $subject;
} else {
$search = strval($search);
$replace = strval($replace);
// Get position of first $search
$pos = strpos($subject, $search);
// Return $subject if $search cannot be found
if ($pos === false) {
return $subject;
}
// Get length of $search, to make proper replacement later on
$search_len = strlen($search);
// Loop until $search can no longer be found, or $limit is reached
for($i = 0; (($i<$limit) || ($limit === -1)); $i++) {
// Replace
$subject = substr_replace($subject, $replace, $pos, $search_len);
// Increase $count
$count++;
// Get location of next $search
$pos = strpos($subject, $search);
// Break out of loop if $needle
if ($pos === false) {
break;
}
}
// Return new $subject
return $subject;
}
}