If you want to divide also multi-zone spaces into separate spaces, trim the spaces before and after and also allow the semicolon ';' instead of just a comma ',', then this code worked for me. I am sure this can be done in one preg_split and / or preg_replace, though!
$string = ' 1,2,3
4,5,;,,
;6,
7
, 8 ,9; 10 ; 11;';
$tmp = preg_split("/[\r\n,;]+/", $string, -1, PREG_SPLIT_NO_EMPTY);
foreach($tmp as $val)
{
$val = trim(preg_replace('!\s+!', ' ', $val));
if($val != "")
$data[] = $val;
}
var_dump($data);
/* Returns
array(11) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(1) "3"
[3]=>
string(1) "4"
[4]=>
string(1) "5"
[5]=>
string(1) "6"
[6]=>
string(1) "7"
[7]=>
string(1) "8"
[8]=>
string(1) "9"
[9]=>
string(2) "10"
[10]=>
string(2) "11"
}
*/
source
share