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

PHP string function example: string splitting

PHPz
PHPzOriginal
2023-06-20 13:58:371905browse

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)

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);

Output result:

Array
(
    [0] => apple
    [1] => banana
    [2] => pear
    [3] => orange
)
  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 )

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);

Output result:

Array
(
    [0] => h
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] =>  
    [6] => w
    [7] => o
    [8] => r
    [9] => l
    [10] => d
)
  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 )

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(",");
}

Output result:

apple
banana
pear
orange

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!

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