微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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)); 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐