Page 119 - Open Soource Technologies 304.indd
P. 119

Unit 5: Strings



            To retrieve the rest of the tokens, repeatedly call strtok( ) with only the separator:  Notes
            $next_chunk = strtok(separator);
            For instance, consider this invocation:
            $string = “Pradip, Kumar, 35, Riya”; $token = strtok($string, “,”); while ($token !== false) {
            echo(“$token<br>”); $token = strtok(“,”); } Pradip Kumar 35 Riya
            The strtok( ) function returns false when there are no more tokens to be returned.
            Call strtok( ) with two arguments to reinitialize the iterator. This restarts the tokenizer from the
            start of the string.
            sscanf ( )
            The sscanf( ) function decomposes a string according to a printf( )-like template:

            $array = sscanf(string, template); $count = sscanf (string, template, var 1, ... );
            If used without the optional variables, sscanf( ) returns an array of fields:
            $string = “Pradip\tKumar (35)”; $a = sscanf($string, “%s\t%s (%d)”); print_r($a);Array ( [0] =>
            Pradip [1] => Kumar[2] => 35 )
            Pass references to variables to have the fields stored in those variables. The number of fields
            assigned is returned:
            $string = “Pradip\tKumar (35)”; $n = sscanf($string, “%s\t%s (%d)”, &$first, &$last, &$age); echo
            “Matched n fields: $first $last is $age years old”; Pradip Kumar is 35 years old.
            5.7.4 String-Searching Functions

            Several functions find a string or character within a larger string. They come in three families:
            strpos( ) and strrpos( ), which return a position; strstr( ), strchr( ), and friends, which return the
            string they find; and strspn( ) and strcspn( ), which return how much of the start of the string
            matches a mask.

            In all cases, if you specify a number as the “string” to search for, PHP treats that number as the
            ordinal value of the character to search for. Thus, these function calls are identical because 44 is
            the ASCII value of the comma:
            $pos = strpos($large, “,”); // find last comma $pos = strpos($large, 44); // find last comma
            All the string-searching functions return false if they cannot find the substring you specified. If
            the substring occurs at the start of the string, the functions return 0. Because falsecasts to the
            number 0, always compare the return value with === when testing for failure:
            if ($pos === false) { // was not found } else { // was found, $pos is offset into string }

            Searches Returning Position
            The strpos( ) function finds the first occurrence of a small string in a larger string:
            $position = strpos(large_string, small_string);
            If the small string is not found, strpos( ) returns false.

            The strrpos( ) function finds the last occurrence of a character in a string. It takes the same
            arguments and returns the same type of value as strpos( ).

            For instance:
            $record  =  “Pradip,  Kumar,  35,  Riya”;  $pos  =  strrpos($record,  “,”);  //  find  last  comma
            echo(“The last comma in the record is at position $pos”); The last comma in the record is at
            position 18.



                                             LOVELY PROFESSIONAL UNIVERSITY                                   113
   114   115   116   117   118   119   120   121   122   123   124