Home > Backend Development > PHP Tutorial > PHP string function example: string splitting

PHP string function example: string splitting

PHPz
Release: 2023-06-20 14:00:02
Original
1943 people have browsed it

There are many string functions in PHP, among which the string splitting function is very commonly used. The string split function can split a string according to the specified delimiter and return an array. Below we will introduce several commonly used string splitting functions.

  1. explode function

The explode function can split the string according to the specified delimiter and return an array. The syntax is as follows:

explode(string $separator , string $string , int $limit = PHP_INT_MAX)
Copy after login

Parameter explanation:

  • $separator: required, specifies the separator, which can be a string or a string array.
  • $string: Required, specifies the string to be split.
  • $limit: Optional, specifies the maximum length of the returned array. Default is PHP_INT_MAX.

Sample code:

$str = "apple,banana,pear,orange";
$arr = explode(",", $str);
print_r($arr);
Copy after login

Output result:

Array
(
    [0] => apple
    [1] => banana
    [2] => pear
    [3] => orange
)
Copy after login
  1. str_split function

The str_split function can convert the string according to Divides by the specified length and returns an array. The syntax is as follows:

str_split ( string $string , int $split_length = 1 )
Copy after login

Parameter explanation:

  • $string: Required, specify the string to be split.
  • $split_length: Optional, specify the length of each element, the default is 1.

Sample code:

$str = "hello world";
$arr = str_split($str);
print_r($arr);
Copy after login

Output result:

Array
(
    [0] => h
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] =>  
    [6] => w
    [7] => o
    [8] => r
    [9] => l
    [10] => d
)
Copy after login
  1. strtok function

The strtok function can convert the string according to Split with the specified delimiter and return the first split substring. The syntax is as follows:

strtok ( string $string , string $token )
Copy after login

Parameter explanation:

  • $string: Required, specify the string to be split.
  • $token: required, specify the delimiter.

Sample code:

$str = "apple,banana,pear,orange";
$tok = strtok($str, ",");
while ($tok !== false) {
    echo "$tok<br>";
    $tok = strtok(",");
}
Copy after login

Output result:

apple
banana
pear
orange
Copy after login

Through the above example, we can see that using the string splitting function can quickly and easily split strings for processing. In actual development, we need to choose different string splitting functions according to different needs to achieve the best processing effect.

The above is the detailed content of PHP string function example: string splitting. For more information, please follow other related articles on the PHP Chinese website!

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