在php中我们要把字符串转换在数组可使用函数有str_split()、explode(),preg_split()函数了,如果把数组转换在字符串我们也有一个函数implode()函数与直接把数组连接起来。
我们先来看字符串转换成数组
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));
?>
结果
Array
(
[0] => Hello
[1] => world.
[2] => It's
[3] => a
[4] => beautiful
[5] => day.
)
preg_split()函数
立即学习“PHP免费学习笔记(深入)”;
代码如下 | 复制代码 |
$user_info = "+J+++G+++++w"; |
总结在php中str_split()、explode()函数功能是一样的,这里就不介绍了。
把数组转换在字符串
implode()
例
代码如下 | 复制代码 |
$array = array('a','b','c'); echo implode($array); //结果 abc |
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号