PHP is a function that converts a comma space carriage return delimited string into an array
- 2020-05-17 04:58:28
- OfStack
When we search for 1, we often encounter the ability to enter multiple criteria separated by Spaces. I happened to have this problem in my project today, so I wrote a function that put multiple conditions into an array. Currently support space, comma (Chinese and English), carriage return segmentation, if not meet the requirements, see this function should be modified 1
<?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));