PHP splits a string into a smaller string (tag) of the function strtok ()

Source: Internet
Author: User
Tags strtok

Instance

To split a string by word:

In the following example, note that we used the string parameter only the first time the Strtok () function was called. After the first call, the function only needs the split parameter, because it knows where it is in the current string. To split a new string, call Strtok () with the string parameter again:

<?php$string = "Hello World". Beautiful Day today. "; $token = Strtok ($string, ""), while ($token! = False) {echo "$token <br>"; $token = Strtok ("");}? >

Definition and usage

The Strtok () function splits a string into smaller strings (tokens).

Grammar

Strtok (String,split)
Parameters Describe
String Necessary. Specifies the string to be split.
Split Necessary. Specifies one or more split characters.

Technical details

return value: Returns a string token.
PHP version: 4 +

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. <br/> "; } echo "-------------<br/>";

Return:

This is the PHP section of the column H Admin 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 <br/>"; $tok = Strtok (""); Space//if ($n >2) break; Can jump out at any time. $n + +; } echo "-------------<br/>";

Return:

This is P P Forum Forum Section Forum Forum Member H Admin Forum members-------------

Example 3: Demonstrates multi-delimiter characters.

$string = "This Is\tan example\nstring"; $tok = Strtok ($string, "\n\t"); #空格, newline, TAB while ($tok!== false) {echo "$tok <br/>"; $tok = Strtok ("\n\t");} 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 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 <br>\n");}

Return:

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.