The advantages of the Strtok () function in PHP are detailed analysis

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags analysis change control cut in demand echo example forum

The Strtok () function can control the tempo relative to explode (). Cut strings on demand. The advantages are:

1. You can define multiple separators at once. 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 with strtok () traversal, you can change the separator at any time, you can break out at any time to stop cutting.

Example 1: Demo to cut with Chinese +explode

$string = "This is the PHP Forum Forum Forums Forum Column Forum H Administrator Forum members";
$arr = explode ("forum", $string);
foreach ($arr as $v)
{
Echo $v. <br/> ";
}
echo "-------------<br/>";

Returns:

This is PHP

Section
Column
H Admin
Member
-------------

Example 2: Demonstrates a replacement cutter, noting that the "H" separator is no longer in the later 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 panes
$n = 1;
while ($tok!== false) {
echo "$tok <br/>";
$tok = Strtok (""); Spaces
if ($n >2) break; Can jump at any time.
$n + +;
}
echo "-------------<br/>";


Returns:

This is P
P Forum
Forum section
Forum columns
Forum h Admin
Forum members
-------------


Example 3: Demonstrates multiple separators.

$string = "This istan examplenstring";
$tok = Strtok ($string, "NT"); #空格, linefeed, TAB
while ($tok!== false) {
echo "$tok <br/>";
$tok = Strtok ("NT");
}
echo "-------------<br/>";


Returns:

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


$string = "ABCDE 123c4 99sadbc99b5232";
$tok = Strtok ($string, "BC");
while ($tok!= "") {
echo "$tok <br/>";
$tok = strtok ("BC");
}
echo "-------------<br/>";


Returns:

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


Example 4: Demonstrates traversing with for:

$line = "leontatkinsontleon@clearink.com";
for ($token = Strtok ($line, "T") $token!= ""; $token =strtok ("T"))
{
Print ("token: $token <br>n");
}


Returns:

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

Related Article

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.