PHP converts a string separated by commas, spaces, and carriage return to an array function. When searching for something, we often encounter the goal of inputting multiple conditions through spaces. Today, when we encounter this problem in the project, we write a function. when we search for something, we often encounter the goal of inputting multiple conditions through spaces. Today, I encountered this problem in the project. I wrote a function and put multiple conditions in the array. Currently, space, comma (Chinese and English), and carriage return are supported. if this function cannot meet your requirements, you can modify it.
The code is 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 ));
Bytes. Today, I encountered this problem in the project. I wrote a function that would add more...