note 43610 deleted from function.str-replace by aidan
| From: | aidan@php.net | Date: | Mon, 28 Jun 2004 10:43:20 +0000 |
| Subject: | note 43610 deleted from function.str-replace by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-71993@lists.php.net to get a copy of this message | ||
Note Submitter: Jeroen - http://jeroen.all-stars.nl
----
README for function str_replace_clean - see below.
Syntax:
function str_replace_clean(mixed $search, mixed $replace, mixed $content, boolean $cs)
Explanation:
If both the $search and $replace parameters are arrays, standard function str_replace() will apply
the substitutions incrementally, that is, it will try the first substitution, then the second using
the result from the first one, and so on. That may be okay, or it may be a problem if you only want
to change the original text.
For example, consider this code:
$search = array("one", "two", "three");
$replace = array("two", "three", "one");
$subject = "one two three";
str_replace($search, $replace, $subject) returns "one one one".
Function str_replace_clean() makes the substitutions only to the original text. For the above
example, str_replace_clean($search, $replace, $subject) would return "two three one".
The basis for this function comes from the contribution by Xavier Paz on www.php.net/str_replace
(30-Dec-2003).
I modified it so that all parameters can be arrays or strings, and I added an option for
case-sensitive comparison or not (standard function str_replace() always compares case-sensitive).
Syntax to call the function:
<?php
require_once('./CReplace.php');
$oReplace = new CReplace;
//Call for case-sensitive comparison:
$result=$oReplace->str_replace_clean($search, $replace, $subject);
or
$result=$oReplace->str_replace_clean($search, $replace, $subject, true);
//Call for case-insensitive comparison:
$result=$oReplace->str_replace_clean($search, $replace, $subject, false);
?>
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.
If $search and $replace are arrays, then str_replace() takes a value from each array and uses them
to do search and replace on $subject.
If $replace has fewer values than $search, then an empty string is used for the rest of the
replacement values.
If $search is an array and $replace is a string, then this replacement string is used for every
value of $search.
$cs is a boolean for case-sensitive comparison.
Default $cs is true to compare and replace strings case-sensitive.
If $cs is false strings will be compared lower case. The replaced result however will still be
written case-sensitive.
$subject_out is used internally to pass the result as the function is iterative.
In class CReplace (see below) you'll find public function str_replace_clean() which you can use
in your code, and private function do_while_for_str_replace_clean(), which should only be called by
function str_replace_clean(), and not directly from your code. That's why it's private.
The functions are written in an object-oriënted style. If you do not want to use these functions as
methods in a class,
remove all ' $this-> ' inside the functions, and remove 'public' and
'private' before 'function'.