Page 120 - Beginning PHP 5.3
P. 120

Part II: Learning the Language
                   The function takes three arguments: the search string, the replacement string, and the string to search
                 through. It returns a copy of the original string with all instances of the search string swapped with the
                 replacement string. Here ’ s an example:

                    $myString = “It was the best of times, it was the worst of times,”;
                    // Displays “It was the best of bananas, it was the worst of bananas,”
                    echo str_replace( “times”, “bananas”, $myString );

                   If you want to know how many times the search string was replaced, pass in a variable as an optional
                 fourth argument. After the function runs, this variable holds the number of replacements:
                    $myString = “It was the best of times, it was the worst of times,”;

                    // Displays “It was the best of bananas, it was the worst of bananas,”
                    echo str_replace( “times”, “bananas”, $myString, $num ) . “ < br/ > ”;

                    // Displays “The text was replaced 2 times.”
                    echo “The text was replaced $num times. < br/ > ”;

                      You can pass arrays of strings for the first and second arguments to search for and replace multiple
                    strings at once. You can also pass an array of strings as the third argument, in which case   str_
                    replace()  replaces the text in all the strings in the array and returns an array of altered strings. This
                    is a very powerful way to do a global search and replace. You learn all about arrays in the next chapter.

                  Replacing a Portion of a String with substr_replace()

                  Whereas  str_replace()  searches for a particular string of text to replace,  substr_replace()  replaces
                 a specific portion of the target string. To use it, pass three arguments: the string to work on, the
                 replacement text, and the index within the string at which to start the replacement.   substr_replace()
                 replaces all the characters from the start point to the end of the string with the replacement text,
                 returning the modified string as a copy (the original string remains untouched).

                   This example shows how   substr_replace()  works:

                    $myString = “It was the best of times, it was the worst of times,”;

                    // Displays “It was the bananas”
                    echo substr_replace( $myString, “bananas”, 11 ) . “ < br/ > ”;

                   You can see that the preceding code has replaced all of the original text from the character at index 11
                 onwards with the replacement text (  “bananas”  ).
                  If you don ’ t want to replace all the text from the start point to the end of the string, you can specify an
                optional fourth argument containing the number of characters to replace:

                    $myString = “It was the best of times, it was the worst of times,”;
                    // Displays “It was the best of bananas, it was the worst of times,”

                    echo substr_replace( $myString, “bananas”, 19, 5 ) . “ < br/ > ”;

              82





                                                                                                      9/21/09   8:53:42 AM
          c05.indd   82
          c05.indd   82                                                                               9/21/09   8:53:42 AM
   115   116   117   118   119   120   121   122   123   124   125