svn: /phpdoc/en/trunk/reference/strings/functions/ str-replace.xml

From: Date: Fri, 16 Oct 2009 18:33:27 +0000
Subject: svn: /phpdoc/en/trunk/reference/strings/functions/ str-replace.xml
Groups: php.doc.cvs 
Request: Send a blank email to doc-cvs+get-5156@lists.php.net to get a copy of this message
philip Fri, 16 Oct 2009 18:33:27 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=289704 Log: - 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 Bug: http://bugs.php.net/49796 (Open) Looks like something's missing here Changed paths: U phpdoc/en/trunk/reference/strings/functions/str-replace.xml Modified: phpdoc/en/trunk/reference/strings/functions/str-replace.xml =================================================================== --- phpdoc/en/trunk/reference/strings/functions/str-replace.xml 2009-10-16 17:43:56 UTC (rev 289703) +++ phpdoc/en/trunk/reference/strings/functions/str-replace.xml 2009-10-16 18:33:27 UTC (rev 289704) @@ -50,6 +50,8 @@ <term><parameter>search</parameter></term> <listitem> <para> + The value being searched for, otherwise known as the <emphasis>needle</emphasis>. + An array may be used to designate multiple needles. </para> </listitem> </varlistentry> @@ -57,6 +59,8 @@ <term><parameter>replace</parameter></term> <listitem> <para> + The replacement value that replaces found <parameter>search</parameter> + values. An array may be used to designate multiple replacements. </para> </listitem> </varlistentry> @@ -64,6 +68,10 @@ <term><parameter>subject</parameter></term> <listitem> <para> + The string or array being searched and replaced on, + otherwise known as the <emphasis>haystack</emphasis>. + </para> + <para> If <parameter>subject</parameter> is an array, then the search and replace is performed with every entry of <parameter>subject</parameter>, and the return value is an array as @@ -139,7 +147,7 @@ &reftitle.examples; <para> <example> - <title><function>str_replace</function> examples</title> + <title>Basic <function>str_replace</function> examples</title> <programlisting role="php"> <![CDATA[ <?php @@ -157,18 +165,37 @@ $newphrase = str_replace($healthy, $yummy, $phrase); -// Use of the count parameter is available as of PHP 5.0.0 +// Provides: 2 $str = str_replace("ll", "", "good golly miss molly!", $count); -echo $count; // 2 - +echo $count; +?> +]]> + </programlisting> + </example> + </para> + <para> + <example> + <title>Examples of potential <function>str_replace</function> gotchas</title> + <programlisting role="php"> +<![CDATA[ +<?php // Order of replacement $str = "Line 1\nLine 2\rLine 3\r\nLine 4\n"; $order = array("\r\n", "\n", "\r"); $replace = '<br />'; + // 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 @@ <refsect1 role="notes"> &reftitle.notes; &note.bin-safe; + <caution> + <title>Replacement order gotcha</title> + <para> + Because <function>str_replace</function> replaces left to right, it might + replace a previously inserted value when doing multiple replacements. + See also the examples in this document. + </para> + </caution> <note> <para> This function is case-sensitive. Use <function>str_ireplace</function>

« previous php.doc.cvs (#5156) next »