Home > Backend Development > PHP Tutorial > Split string using strtok() function in PHP

Split string using strtok() function in PHP

王林
Release: 2023-06-27 09:08:03
Original
1156 people have browsed it

In PHP programming, string processing is a frequently used function. One of the most commonly used string manipulation functions is strtok(). The strtok() function can split a string into several substrings by specifying the delimiter. In this article, we will introduce the relevant knowledge and usage of strtok() function.

1. Basic usage of strtok() function

strtok() function contains two parameters. The first parameter is the string to be split, and the second parameter is the specified delimiter. . The return value of the function is the currently divided substring.

Code example:

$str = "Hello, World! How are you today?";
$delimiter = " ,!";

$token = strtok($str, $delimiter);

while ($token !== false) {
    echo "$token<br>";
    $token = strtok($delimiter);
}
Copy after login

In the above example, we use the strtok() function to split the $str string into multiple substrings using commas, spaces, and exclamation marks as separators. The execution results are as follows:

Hello
World
How
are
you
today?
Copy after login

In the above code, we use a while loop. Each loop uses the strtok() function to obtain a substring $token and then output it. The function returns false to indicate that there are no more substrings.

2. Detailed analysis of strtok() function

The complete definition of strtok() function is:

string strtok ( string $str, string $delimiter )
Copy after login

Among them:

  • $str: The string to split.
  • $delimiter: Specified delimiter.

The return value of the strtok() function:

  • When called for the first time, the first non-delimiter substring in $str is returned.
  • On subsequent calls, return the remaining non-delimited substring.
  • If there are no more non-delimiter substrings, return false.

When calling the strtok() function, PHP will traverse the $str string from left to right and cut it according to the $delimiter delimiter. Each time the function is called, a substring will be returned, and the internal pointer will automatically move backwards to point to the beginning of the next substring. This means that every time the function is called, a new substring will be returned.

But it should be noted that to obtain all delimiters, you need to call the strtok() function repeatedly until false is returned.

Code example:

$str = "this,is,a,comma,separated,string";
$delimiter = ",";

$token = strtok($str, $delimiter);

while ($token !== false) {
    echo "$token<br>";
    $token = strtok($delimiter);
}
Copy after login

In the above example, we use the strtok() function to split the $str string into multiple substrings using commas as the separator. The execution result is as follows:

this
is
a
comma
separated
string
Copy after login

In this example, we set the separator to comma. When the strtok() function is called for the first time, it returns the first substring "this" in the string we want to split. Then we call the strtok() function repeatedly, returning a new substring each time until false is returned.

3. Notes on the strtok() function

  1. Back up the original string

When we use the strtok() function to split a string, The function modifies the original string. If you need to use the original string in subsequent programs, you need to back it up first.

Code example:

$str = "first,second,third";
$delimiter = ",";
$original_str = $str;

$token = strtok($str, $delimiter);

while ($token !== false) {
    echo "$token<br>";
    $token = strtok($delimiter);
}

echo $original_str;
Copy after login
  1. The delimiter can only be one character

It should be noted that the $delimiter parameter can only be one character, otherwise Each character will be treated as waiting to be passed.

Code example:

$str = "one,two,three";
$delimiter = ",e";

$token = strtok($str, $delimiter);

while ($token !== false) {
    echo "$token<br>";
    $token = strtok($delimiter);
}
Copy after login

The expected effect of this code is to separate the string by comma and the letter e, but in fact the function does not use the letter e as the separator, it will The character "e" is regarded as a waiting separator, which means that the two adjacent characters must be separators for normal use.

  1. The impact of parameter order

In the strtok() function, the first parameter $str specifies the string to be split, and the second parameter is The specified delimiter. The order of these two parameters cannot be reversed. If the order of these two parameters is reversed, an error will be reported.

Code example:

$str = "one,two,three";
$delimiter = ",";

// 错误方式
$token = strtok($delimiter, $str);

while ($token !== false) {
    echo "$token<br>";
    $token = strtok($delimiter);
}
Copy after login

4. Summary

In PHP programming, there are many scenarios where string operations are used. The strtok() function is a very convenient string splitting function that can split a string into several substrings according to the specified delimiter.

When using the strtok() function, pay attention to the type of function return value, backup of the original string, the delimiter can only be one character, and the impact of the order of parameters. Proper use of the strtok() function can help us process strings more efficiently.

The above is the detailed content of Split string using 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