Page 123 - Beginning PHP 5.3
P. 123

Chapter 5: Strings
































                                   Figure 5 - 2





                             H ow  I t  W orks
                           The script starts by displaying an XHTML page header, then defining a variable,  $myText , containing the
                         text to justify. The text is included in the script using the heredoc syntax. (The extra blank line at the end
                         of the text ensures that the last line of the text has a newline character at the end of it; this is required by
                         the algorithm that the script uses.)
                           After defining   $myText , the script uses  str_replace()  to convert any Windows line endings
                         (a carriage return followed by a line feed) into UNIX line endings (a line feed on its own). Windows
                         line endings can occur if the script file was saved on a Windows machine, and they can confuse the
                         justification algorithm (which expects each line to end with just a line feed):

                          $myText = str_replace( “\r\n”, “\n”, $myText );
                           Next, the script sets a few more variables:

                            ❑       $lineLength  : The desired length that you ’ d like each line of text to be. Try changing this to
                                different values to see what happens
                            ❑       $myTextJustified  : This will contain the final, justified text







                                                                                                          85





                                                                                                      9/21/09   8:53:43 AM
          c05.indd   85
          c05.indd   85                                                                               9/21/09   8:53:43 AM
   118   119   120   121   122   123   124   125   126   127   128