Page 121 - Beginning PHP 5.3
P. 121

Chapter 5: Strings
                           Pass a negative fourth argument to replace up to that many characters from the end of the string:

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

                             echo substr_replace( $myString, “bananas”, 19, -20 ) . “ < br/ > ”;
                           You can also pass a zero value to insert the replacement text into the string rather than replacing
                         characters:
                             $myString = “It was the best of times, it was the worst of times,”;

                             // Displays “It really was the best of times, it was the worst of times,”

                             echo substr_replace( $myString, “really “, 3, 0 ) . “ < br/ > ”;





                           Try It Out   Justifying Text
                           You can use the string functions you ’ ve learned so far to write a script to justify lines of text.  Justifying
                         text means aligning text within a column so that the text is flush with both the left and right margins.
                          Here ’ s the script. Save it as   justification.php  in your document root folder:
                            < !DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
                            “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd” >
                            < html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en” >
                             < head >
                               < title > Justifying Lines of Text < /title >
                               < link rel=”stylesheet” type=”text/css” href=”common.css” / >
                             < /head >
                             < body >
                               < h1 > Justifying Lines of Text < /h1 >

                            < ?php
                          // The text to justify

                          $myText =  < < < END_TEXT


                          But think not that this famous town has
                          only harpooneers, cannibals, and
                          bumpkins to show her visitors. Not at
                          all. Still New Bedford is a queer place.
                          Had it not been for us whalemen, that
                          tract of land would this day perhaps
                          have been in as howling condition as the
                          coast of Labrador.
                          END_TEXT;

                          $myText = str_replace( “\r\n”, “\n”, $myText );
                          $lineLength = 40; // The desired line length
                          $myTextJustified = “”;


                                                                                                          83





                                                                                                      9/21/09   8:53:43 AM
          c05.indd   83
          c05.indd   83                                                                               9/21/09   8:53:43 AM
   116   117   118   119   120   121   122   123   124   125   126