Home > Backend Development > PHP Tutorial > PHP function to convert a string separated by commas, spaces, and carriage returns into an array_PHP tutorial

PHP function to convert a string separated by commas, spaces, and carriage returns into an array_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-21 15:18:04
Original
1083 people have browsed it

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

www.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...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template