Unlike the previous article & quot; explode () function usage & quot;, explode () splits a string into several small blocks each time, strtok () the function extracts only some fragments from the string at a time. For processing a word extracted from a string at a time, the strtok () function is better than The explode () function.
With the previous article"Explode () function usage", Explode () in each split a string into several small pieces, the strtok () function only extracts some fragments from the string at a time. For processing a word extracted from a string at a time, the strtok () function is better than The explode () function.
By querying the PHP development manual, you can see that the prototype of the strtok () function is as follows:
String strtok (string input, string separator );
The delimiter can be either a character or a string, but note that the characters we enter are separated based on each character of the delimiter string, instead of being separated by the entire separator string, this explode () function is the same.
The call of function strtok () is not as simple as that of other function prototypes. To obtain the first segment from a string, you can call the strtok () function with two input parameters: one is the string to be segmented, and the other is the separator. To obtain the segment sequence from a string, you can use only one parameter-separator. This function will keep its internal pointer position in the string. If you want to reset the pointer, you can re-pass the string to this function.
The application example of the strtok () function is as follows:
$ Token = strtok ($ feedback ,'"");
Echo $ token ."
";
While ($ token! = "");
Echo $ token ."
";
}
Generally, it is a good idea to use functions like empty () to check whether the user has actually entered feedback in the submitted form. These checks are not described in detail here.
The code above splits each piece of information in the user feedback into fragments and prints them on each row, until there are no longer separated fragments. In this process, empty strings are automatically skipped.