Home > Article > Backend Development > PHP splits a string and outputs an array of characters example_PHP tutorial
The explode() function can be used to split character functions in php, but there must be a rule for using this function, such as | separated by other characters, so that we can directly use explode to divide the string into an array and then use for to traverse the output. Let's look at a few examples.
The explode() function splits a string into an array.
Grammar
explode(separator,string,limit)
Example 1
The code is as follows | |||||
$test='472347127,893372115,850965403';
|
Output: 0.472347127 1.893372115 2.850965403
Example 2
The code is as follows | |||||
$a="893372115,472347127,850965403" ;
|
Output: 893372115 472347127 850965403 PHP
Comma splits string
Use explode function to split string into array
The code is as follows | |
| |
代码如下 | |
$source = "hello1,hello2,hello3,hello4,hello5";//按逗号分离字符串 for($index=0;$index ?> |
$source = "hello1,hello2, hello3,hello4,hello5";//Separate strings by commas
$hello = explode(',',$source);
for($index=0;$ index
}
?>
split函数进行字符分割
代码如下 | |
| |
代码如下 | |
// 分隔符可以是斜线,点,或横线 |
// 分隔符可以是斜线,点,或横线
$date = "04/30/1973";
list($month, $day, $year) = split ('[/.-]', $date);
echo "Month: $month; Day: $day; Year: $year
n";
?>