Home > Backend Development > PHP Problem > How to use php strtok function?

How to use php strtok function?

藏色散人
Release: 2023-02-22 22:42:01
Original
2893 people have browsed it

php The strtok function is used to split a string into smaller strings (marks). Its syntax is strtok (string, split). The parameter string is required and specifies the string to be split; split is required and specifies one or more separator characters.

How to use php strtok function?

#php How to use strtok function?

Definition and usage

strtok() function splits a string into smaller strings (tokens).

Syntax

strtok(string,split)
Copy after login

Parameters

string Required. Specifies the string to be split.

split Required. Specifies one or more delimiting characters.

Return value: Returns the string token.

PHP Version: 4

Example

Split the string one by one:

In the following example, please note that we only The first time the strtok() function is called, the string parameter is used. After the first call, the function only requires 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(" ");
}
?>
Copy after login

Output:

Hello
world.
Beautiful
day
today.
Copy after login

The above is the detailed content of How to use php strtok function?. 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