note 77707 deleted from function.str-replace by felipe
| From: | felipe@php.net | Date: | Sun, 23 Mar 2008 23:06:11 +0000 |
| Subject: | note 77707 deleted from function.str-replace by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-137425@lists.php.net to get a copy of this message | ||
Note Submitter: Guy Gordon
----
The exact meaning of "all occurrences" should be documented. Str_replace does not include
any replaced text in subsequent searches. (The search for the next occurrence starts after the
replace, not at the beginning of the string.) This can lead to the result string retaining
occurrences of the search string.
For example, if you want to replace all multiple spaces with a single space, a simple str_replace
will not work:
$strIn = ' '; //six spaces
echo str_replace(' ',' ',$strIn); //will echo three spaces
Instead, you could use:
while (strpos($strIn,' ') !== False)
$strIn = str_replace(' ',' ',$strIn);