When we search for something, we often encounter the possibility of entering multiple conditions separated by spaces. I happened to encounter this situation in my project today, so I wrote a function to put multiple conditions into an array. Currently, it supports space, comma (Chinese and English), and carriage return separation. If it cannot meet the needs, it should be enough to take a look at this function and modify it
Copy code Code As follows:
/**
* 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) );
http://www.bkjia.com/PHPjc/325599.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325599.htmlTechArticleWhen we search for something, we often encounter the possibility of entering multiple conditions separated by spaces. I happened to encounter this situation in the project today, so I wrote a function that will...