Home > php教程 > php手册 > php数组与字符串的转换函数

php数组与字符串的转换函数

WBOY
Release: 2016-06-13 11:15:45
Original
1467 people have browsed it

在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()函数

 代码如下 复制代码

$user_info = "+J+++G+++++w";
$fields = preg_split("/+{1,}/", $user_info);
while ($x    print $fields[$x]. "
";
   $x++;
endwhile;
?>

总结在php中str_split()、explode()函数功能是一样的,这里就不介绍了。

把数组转换在字符串

implode()


 代码如下 复制代码

$array = array('a','b','c');

echo implode($array);

//结果  abc


source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template