在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 < sizeof($fields)) :
print $fields[$x]. "
";
$x++;
endwhile;
?>
總結在php中str_split()、explode()函數功能是一樣的,這裡就不介紹了。
把數組轉換在字符串
implode()
例
$array = array('a','b','c');
echo implode($array);
//結果 abc