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