Advantages of PHP strtok () Functions

Source: Internet
Author: User
Tags strtok

Its advantages are:

1. multiple separators can be defined at a time. When a function is executed, it is cut by a single separator rather than the entire separator, while explode is cut by the entire separator string. Therefore, explode can be cut in Chinese, while strtok won't. It will be garbled.

2. When using while or for with strtok (), you can change the separator at any time or use break to exit and terminate the cut at any time.

Example 1: show how to use Chinese + explode to cut

$ String = "this is a PHP forum Forum topic forum H administrator forum member ";
$ Arr = explode ("Forum", $ string );
Foreach ($ arr as $ v)
{
Echo $ v. "<br/> ";
}
Echo "------------- <br/> ";

Return Value:

This is PHP

Forum
Topic
H Administrator
Member
-------------

Example 2: replace the cut character. Note that the separator "H" is not contained in the WHILE character. It only uses spaces.

$ String = "this is a PHP forum Forum topic forum H administrator forum member ";
$ Tok = strtok ($ string, "H"); // space + H
$ N = 1;
While ($ tok! = False ){
Echo "$ tok <br/> ";
$ Tok = strtok (""); // Space
// If ($ n> 2) break; // you can jump out at any time.
// $ N ++;
}
Echo "------------- <br/> ";

Return Value:

This is P
P Forum
Forum
Forum topic
Forum H Administrator
Forum members
-------------

Example 3: Multiple delimiters are displayed.

$ String = "This is \ tan example \ nstring ";
$ Tok = strtok ($ string, "\ n \ t"); # space, line feed, TAB
While ($ tok! = False ){
Echo "$ tok <br/> ";
$ Tok = strtok ("\ n \ t ");
}
Echo "------------- <br/> ";

Return Value:

This
Is
An
Example
String
-------------

$ String = "abcde 123c4 99sadbc99b5232 ";
$ Tok = strtok ($ string, "bc ");
While ($ tok! = ""){
Echo "$ tok <br/> ";
$ Tok = strtok ("bc ");
}
Echo "------------- <br/> ";

Return Value:

A
De 123
4 99sad
99
5232
-------------

Example 4: Use for traversal:

$ Line = "leon \ tatkinson \ tleon@clearink.com ";
For ($ token = strtok ($ line, "\ t"); $ token! = ""; $ Token = strtok ("\ t "))
{
Print ("token: $ token <BR> \ n ");
}

Return Value:

Token: leon
Token: atkinson
Token: leon@clearink.com

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.