Use of the strtok () function for string segmentation

Source: Internet
Author: User
Tags strtok
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.