Page 116 - Beginning PHP 5.3
P. 116

Part II: Learning the Language
                   Another useful related function is  str_word_count() , which returns the number of words in a string.
                 For example:
                    echo str_word_count( “Hello, world!” ); // Displays 2


                  Accessing Characters within a String

                   You might be wondering how you can access the individual characters of a string. PHP makes this easy
                 for you. To access a character at a particular position, or  index , within a string, use:
                    $character = $string[index];

                   In other words, you place the index between square brackets after the string variable name. String
                 indices start from   0 , so the first character in the string has an index of  0 , the second has an index of  1 ,
                 and so on. You can both read and change characters this way. Here are some examples:

                    $myString = “Hello, world!”;
                    echo $myString[0] . “ < br / > ”; // Displays ‘H’
                    echo $myString[7] . “ < br / > ”; // Displays ‘w’
                    $myString[12] = ‘?’;
                    echo $myString . “ < br / > ”;    // Displays ‘Hello, world?’

                   If you need to extract a sequence of characters from a string, you can use PHP ’ s  substr()  function. This
                function takes the following parameters:

                   ❑       The string to extract the characters from
                   ❑       The position to start extracting the characters. If you use a negative number,   substr()  counts
                       backward from the end of the string
                   ❑       The number of characters to extract. If you use a negative number,   substr()  misses that many
                       characters from the end of the string instead. This parameter is optional; if left out,   substr()
                       extracts from the start position to the end of the string
                  Here are a few examples that show how to use   substr()  :
                    $myString = “Hello, world!”;
                    echo substr( $myString, 0, 5 ) . “ < br/ > ”;   // Displays ‘Hello’
                    echo substr( $myString, 7 ) . “ < br/ > ”;      // Displays ‘world!’
                    echo substr( $myString, -1 ) . “ < br/ > ”;     // Displays ‘!’




                    echo substr( $myString, -5, -1 ) . “ < br/ > ”; // Displays ‘orld’

                      You can ’ t modify characters within strings using  substr()  . If you need to change characters within a
                    string, use   substr_replace()  instead. This function is described later in the chapter.
                  Searching Strings

                  Often it ’ s useful to know whether one string of text is contained within another. PHP gives you several
                 string functions that let you search for one string inside another:



              78





                                                                                                      9/21/09   8:53:41 AM
          c05.indd   78                                                                               9/21/09   8:53:41 AM
          c05.indd   78
   111   112   113   114   115   116   117   118   119   120   121