利用PHP怎么将字符串转换为数组

这篇文章将为大家详细讲解有关利用PHP怎么将字符串转换为数组,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

具体方法如下:

<?php 
/** 
* transform ' hello, world !' to array('hello', 'world') 
*/ 
function strsToArray($strs) { 
$result = array(); 
$array = array(); 
$strs = str_replace(',', ',', $strs); 
$strs = str_replace("n", ',', $strs); 
$strs = str_replace("rn", ',', $strs); 
$strs = str_replace(' ', ',', $strs); 
$array = explode(',', $strs); 
foreach ($array as $key => $value) { 
if ('' != ($value = trim($value))) { 
$result[] = $value; 
} 
} 
return $result; 
} 
//test 
$strs = 'Code is poetry! WTF!'; 
var_dump(strsToArray($strs));

关于利用PHP怎么将字符串转换为数组就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。