Home >Backend Development >PHP Tutorial >Conversion function between php array and string_PHP tutorial
In PHP, if we want to convert a string into an array, we can use the functions str_split(), explode(), and preg_split(). If we want to convert the array into a string, we also have a function implode() function to directly connect the arrays. .
Let’s first look at converting strings into arrays
str_split()
print_r(str_split("Hello"));
?>
Array
(
[0] => H
[1] => e
[2] => l
[3] => l
[4] => o
)
explode()
$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
?>
Results
Array
(
[0] => Hello
[1] => world.
[2] => It's
[3] => a
[4] => beautiful
[5] => day.
)
preg_split() function
The code is as follows | Copy code | ||||
$fields = preg_split("/+{1,}/", $user_info); while ($x < sizeof($fields)) :Print $fields[$x]. " ";$x++; endwhile;
?> |
代码如下 | 复制代码 |
$array = array('a','b','c'); echo implode($array); //结果 abc |
implode()
http://www.bkjia.com/PHPjc/445612.html