Home  >  Article  >  Backend Development  >  PHP function strtok() that splits a string into smaller strings (tags)

PHP function strtok() that splits a string into smaller strings (tags)

黄舟
黄舟Original
2017-11-06 10:34:071652browse

Example

Split by wordString:

In the example below, please note that we only call strtok## the first time #() function uses the string parameter. After the first call, the function only requires the split parameter because it knows where it is in the current string. If you need 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

strtok() function splits the string into smaller strings (mark).

Syntax

strtok(string,split)

ParametersDescriptionstring Required. Specifies the string to be split. splitRequired. Specifies one or more delimiting characters.
Technical details

Return value: Returns the string token. PHP version: 4+##The advantages are:

1. Multiple delimiters can be defined at one time. When the function is executed, it cuts according to a single delimiter instead of the entire delimiter, while

explode

cuts according to the entire delimiter string. For this reason, explode can be cut in Chinese, but strtok cannot and will be garbled. 2. When using while or for with strtok() to traverse, you can change the separator at any time, or use break to terminate cutting at any time.

Example 1: Demonstrate using Chinese + explode to cut

$string = "这是PHP论坛 论坛版块 论坛栏目 论坛H管理员 论坛会员"; 
$arr = explode("论坛",$string); 
foreach($arr as $v) 
{ 
echo $v."<br />"; 
} 
echo "-------------<br />";

Return:

这是PHP 

版块 
栏目 
H管理员 
会员 
-------------

Example 2: Demonstrate changing the cutting character, please note that there is no longer "H" in WHILE " separator. Instead just use spaces.

$string = "这是PHP论坛 论坛版块 论坛栏目 论坛H管理员 论坛会员"; 
$tok = strtok($string, " H"); //空格+H 
$n=1; 
while ($tok !== false) { 
echo "$tok<br />"; 
$tok = strtok(" "); //空格 
//if($n>2)break; //可以随时跳出。 
//$n++; 
} 
echo "-------------<br />";

Return:

这是P 
P论坛 
论坛版块 
论坛栏目 
论坛H管理员 
论坛会员 
-------------

Example 3: Demonstrate multiple delimiters.

$string = "This is\tan example\nstring"; 
$tok = strtok($string, " \n\t"); #空格,换行,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 
99 
5232 
-------------

Example 4: Demonstrate using 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

The above is the detailed content of PHP function strtok() that splits a string into smaller strings (tags). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn