Heim  >  Artikel  >  Backend-Entwicklung  >  php数组与字符串的转换函数_PHP教程

php数组与字符串的转换函数_PHP教程

WBOY
WBOYOriginal
2016-07-20 10:59:25936Durchsuche

在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


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445612.htmlTechArticle在php中我们要把字符串转换在数组可使用函数有str_split()、explode(),preg_split()函数了,如果把数组转换在字符串我们也有一个函数implode()函数...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn