The Strtok () function can control the tempo relative to explode (). Cut strings as needed. Its advantages are:
1, you can define more than one separator at a time. When a function is executed, it is cut by a single delimiter rather than by the entire separator, while the explode is cut by the entire separator string. Therefore, explode can be cut in Chinese, and strtok is not, will be garbled.
2, in the use while or for the strtok () traversal, you can change the separator at any time, you can break out at any time to stop cutting.
Example 1: Demo +explode in Chinese to cut
$string = "This is the Forum Forum Forums Forum H Administrator Forum members";
$arr = explode ("forum", $string);
foreach ($arr as $v)
{
echo $v. " <br/> ";
}
echo "-------------<br/>";
return:
This is the PHP
Forum
column
H Administrator
member
-------------
Example 2: Demonstrates a replacement cutter, noting that the "H" separator is no longer in the back while. And just use a space.
$string = "This is the PHP Forum Forum Forums Forum Column Forum H Administrator Forum members";
$tok = strtok ($string, "H"); Space +h
$n = 1;
while ($tok!== false) {
echo "$tok <br/>";
$tok = Strtok (""); Space
//if ($n >2) break; Can jump at any time.
//$n + +;
}
echo "-------------<br/>";
return:
, this is P
.
P Forum
Forum
Forum Column
Forum H Administrator
Forum member
-------------
Example 3: Demonstrates multiple separator characters.
$string = "this istan examplenstring";
$tok = strtok ($string, "NT"); #空格, linefeed, TAB
while ($tok!== false) {
echo "$tok <br/>";
$tok = Strtok ("NT");
}
echo "-------------<br/>";
return:
this
is
an
Example
string
-------------
$string = "ABCDE 123c4 99sadbc99b5232";
$tok = strtok ($string, "BC");
while ($tok!= "") {
echo "$tok <br/>";
$tok = strtok ("BC");
}
echo "-------------<br/>";
return:
a
de 123
4 99sad
99
5232
-------------
Example 4: Demo using for to traverse:
$line = "leontatkinsontleon@clearink.com";
for ($token = Strtok ($line, "T"); $token!= "" $token =strtok ("T")
{
Print ("token: $token <br>n");
}
return:
Token:leon
Token:atkinson
token:leon@clearink.com