note 67979 deleted from function.str-replace by felipe
| From: | felipe@php.net | Date: | Sun, 06 Jan 2008 12:48:15 +0000 |
| Subject: | note 67979 deleted from function.str-replace by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-134876@lists.php.net to get a copy of this message | ||
Note Submitter: TLG
----
About xinobit's function -> what if $search, $replace or/and $subject are arrays?.. ;)
I supplemented the code to support that possibility but the result was a bit heavy... and slow!
preg_replace() is about 5 times faster! :) (more or less depending on the length of $subject)
So, a little example of function:
<?php
function str_replace_limit($search, $replace, $subject, $limit=-1) {
// constructing mask(s)...
if (is_array($search)) {
foreach ($search as $k=>$v) {
$search[$k] = '
' . preg_quote($search[$k],'') .
'`';
}
}
else {
$search = '' . preg_quote($search,'') . '`';
}
// replacement
return preg_replace($search, $replace, $subject, $limit);
}
?>
(of course you could insert a "&$count" argument for PHP >= 5.1.0)