From 1b72640345862f505ae6d7fa53ee45bacc8dfb4d Mon Sep 17 00:00:00 2001 From: Philip Olson Date: Fri, 16 Oct 2009 18:33:27 +0000 Subject: [PATCH] - Added parameter descriptions, which closes PHP bug #49796 - Split examples into two: basics and potential gotchas - Integrated information about multiple replacement gotcha (left to right) via a zillion user notes - Added example about said gotcha (via michael dot moussa at gmail dot com) - A few other minor changes git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@289704 c90b9560-bf6c-de11-be94-00142212c4b1 --- reference/strings/functions/str-replace.xml | 43 +++++++++++++++++++-- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/reference/strings/functions/str-replace.xml b/reference/strings/functions/str-replace.xml index 474f368998..819b644459 100644 --- a/reference/strings/functions/str-replace.xml +++ b/reference/strings/functions/str-replace.xml @@ -50,6 +50,8 @@ search + The value being searched for, otherwise known as the needle. + An array may be used to designate multiple needles. @@ -57,12 +59,18 @@ replace + The replacement value that replaces found search + values. An array may be used to designate multiple replacements. 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 @@ -139,7 +147,7 @@ &reftitle.examples; - <function>str_replace</function> examples + Basic <function>str_replace</function> examples +]]> + + + + + + Examples of potential <function>str_replace</function> gotchas + +'; + // Processes \r\n's first so they aren't converted twice. $newstr = str_replace($order, $replace, $str); +// Outputs F because A is replaced with B, then B is replaced with C, and so on... +// Finally E is replaced with F, because of left to right replacements. +$search = array('A', 'B', 'C', 'D', 'E'); +$replace = array('B', 'C', 'D', 'E', 'F'); +$subject = 'A'; +echo str_replace($search, $replace, $subject); + // Outputs: apearpearle pear +// For the same reason mentioned above $letters = array('a', 'p'); $fruit = array('apple', 'pear'); $text = 'a p'; @@ -184,6 +211,14 @@ echo $output; &reftitle.notes; ¬e.bin-safe; + + Replacement order gotcha + + Because str_replace replaces left to right, it might + replace a previously inserted value when doing multiple replacements. + See also the examples in this document. + + This function is case-sensitive. Use str_ireplace