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
|