The difference between explode() function and strtok() function in php

黄舟
Release: 2023-03-17 13:22:01
Original
2527 people have browsed it

php strtok()function and explode() function can both be used to split string, but there are some differences between them The difference is that compared to explode(), the strtok() function can control the rhythm. This article introduces the difference between the explode() function and strtok() function in PHP in detail!

Compared to explode() In other words, the strtok() function can control the rhythm. Cut the string as needed. The advantages are:

1. Multiple separators can be defined at one time. When the function is executed, it cuts by a single delimiter instead of the entire delimiter, while explode cuts by 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: Demonstrates using Chinese explode to cut

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

Running results:

这是PHP 

版块 
栏目 
H管理员 
会员 
-------------
Copy after login

Example 2: Demonstrates 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 />";
Copy after login

Running result:

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

Example 3: Demonstrates 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 />";
Copy after login

Running result:

This 
is 
an 
example 
string 
-------------
Copy after login
$string = "abcde 123c4 99sadbc99b5232"; 
$tok = strtok($string, "bc"); 
while ($tok !="") { 
echo "$tok<br />"; 
$tok = strtok("bc"); 
} 
echo "-------------<br />";
Copy after login

Running result:

a 
de 123 4 99sad 
99 5232 -------------
Copy after login

Example 4: Demonstrates using for to traverse:

$line = "leon\tatkinson\tleon@clearink.com"; 
for($token = strtok($line,"\t");$token!="";$token=strtok("\t")) 
{ 
print("token: $token<BR>\n"); 
}
Copy after login

Running result:

token: leon 
token: atkinson 
token: leon@clearink.com
Copy after login

Summary:

I believe that through studying this article, you will know the difference between strtok() and explode() functions, and you can use them at work. The specific situation depends on the use of a certain function. I hope it will be helpful to your work!

Related recommendations:

Detailed explanation of the application of explode() function in php


Example code of the explode() function in php


Detailed definition of the explode() function in php

The above is the detailed content of The difference between explode() function and strtok() function in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template