Home >Backend Development >PHP Problem >What is the function to split string in php
php functions to split strings include explode() and str_split()
##explode()
This function is the inverse function of implode(). It uses one string to split another string and returns an array.Grammar: (Recommended learning: PHP video tutorial)
array explode( string separator, string string [, int limit] )<br/>
Example:
<?php<br/>$str = 'one|two|three|four';<br/>print_r(explode('|', $str));<br/>print_r(explode('|', $str, 2));<br/>// 负数的 limit(自 PHP 5.1 起)<br/>print_r(explode('|', $str, -1));<br/>?><br/>
The output results are as follows:
Array<br/>(<br/> [0] => one<br/> [1] => two<br/> [2] => three<br/> [3] => four<br/>)<br/>Array<br/>(<br/> [0] => one<br/> [1] => two|three|four<br/>)<br/>Array<br/>(<br/> [0] => one<br/> [1] => two<br/> [2] => three<br/>)<br/>
str_split()
str_split() splits the string into an array and returns an array successfully.Syntax:
array str_split( string string [, int length] )<br/>
Example:
<?php<br/>$str = 'one two three';<br/>$arr1 = str_split($str);<br/>$arr2 = str_split($str, 3);<br/>print_r($arr1);<br/>print_r($arr2);<br/>?><br/>
The output is as follows:
Array<br/>(<br/> [0] => o<br/> [1] => n<br/> [2] => e<br/> [3] => <br/> [4] => t<br/> [5] => w<br/> [6] => o<br/> [7] => <br/> [8] => t<br/> [9] => h<br/> [10] => r<br/> [11] => e<br/> [12] => e<br/>)<br/>Array<br/>(<br/> [0] => one<br/> [1] => tw<br/> [2] => o t<br/> [3] => hre<br/> [4] => e<br/>)<br/>
The above is the detailed content of What is the function to split string in php. For more information, please follow other related articles on the PHP Chinese website!