Preg_match_all is comma separated with values โโthat probably contain spaces
I am currently using preg_match_all
for normal strings that do not contain spaces, but now I need to make it work for anything in between each space.
I need abc, hh, hey there, 1 2 3, hey_there_
to go back
abc
hh
hey there``1 2 3
hey_there_
But my current script just stops when space is consumed.
preg_match_all("/([a-zA-Z0-9_-]+)+[,]/",$threadpolloptions,$polloptions);
foreach(array_unique($polloptions[1]) as $option) {
$test .= $option.' > ';
}
In this case, you don't need regular expression. Explode will be faster
$str = 'abc, hh, hey there, 1 2 3, hey_there_';
print_r(explode(', ', $str));
result
Array
(
[0] => abc
[1] => hh
[2] => hey there
[3] => 1 2 3
[4] => hey_there_
)
UPDATE
$str = 'abc, hh,hey there, 1 2 3, hey_there_';
print_r(preg_split("/,\s*/", $str));
will lead to the same
You can use explode
together with array_map
like
$str = 'abc, hh, hey there, 1 2 3, hey_there_';
var_dump(array_map('trim',explode(',',$str)));
Fiddle
You can use explode ():
$string = "abc, hh, hey there, 1 2 3, hey_there_";
$array = explode(',', $string);
foreach($array as $row){
echo trim($row, ' ');
}