The advantages are:
1. You can define multiple separators at once. When the function is executed, it is cut by a single delimiter rather than by the entire delimiter, while the explode is cut by the entire separator string. As a result, explode can be cut in Chinese, and strtok is not, will be garbled.
2. When using the while or for mate Strtok () traversal, you can change the delimiter at any time, or you can break out of the cut at any time.
Example 1: Demo in Chinese +explode to cut
$string = "This is the PHP Forum Forum Panel Forum Column Forum H Admin Forum member"; $arr = explode ("forum", $string); foreach ($arr as $v) { echo $v. " "; } echo "------------- "; |
Return:
This is PHP Section Column H Administrator Member -------------
|
Example 2: Demonstrate replacement of the cut, note that the "H" delimiter is no longer in the later while. And just use a space.
$string = "This is the PHP Forum Forum Panel Forum Column Forum H Admin Forum member"; $tok = Strtok ($string, "H"); Space +h $n = 1; while ($tok!== false) { echo "$tok "; $tok = Strtok (""); Space if ($n >2) break; Can jump out at any time. $n + +; } echo "------------- "; |
Return:
This is P P Forum Forum section Forum columns Forum h Admin Forum members ------------- |
Example 3: Demonstrates multi-delimiter characters.
$string = "This Is\tan example\nstring"; $tok = Strtok ($string, "\n\t"); #空格, line break, TAB while ($tok!== false) { echo "$tok "; $tok = Strtok ("\n\t"); } echo "------------- "; |
Return:
This Is An Example String ------------- |
$string = "ABCDE 123c4 99sadbc99b5232"; $tok = Strtok ($string, "BC"); while ($tok! = "") { echo "$tok "; $tok = strtok ("BC"); } echo "------------- "; |
Return:
A De 123 4 99sad 99 5232 ------------- |
Example 4: The demo uses for to traverse:
$line = "leon\tatkinson\tleon@clearink.com"; for ($token = Strtok ($line, "\ t"); $token! = ""; $token =strtok ("\ T")) { Print ("token: $token \ n "); } |
Return:
Token:leon Token:atkinson Token:leon@clearink.com
|
The above describes the Strtok php strtok function of the advantages of analysis, including the strtok aspect of the content, I hope that the PHP tutorial interested in a friend helpful.