tags:

views:

137

answers:

2

I have a textarea field on my webpage. I am accepting the user input, I want to parse that user input for "(SEPARATED BY COMMAS, SPACES, OR ONE PER LINE)" this line.

Basically I want to fetch the words, seprated by comma, space or one per line. What can be the RegEx for this which I can use like below:

preg_split('/[,; " "]+/', $_tags);

I am already using a regEx to separate tags entered by the user. What will be the regEx to fetch word from a string which are "(SEPARATED BY COMMAS, SPACES, OR ONE PER LINE)"

Thanks

+4  A: 
<?php  
$_tags = "foo bar, dim; sum\nblah";
var_dump(preg_split('/[;, \n]+/', $_tags));
?>

Results to:

array(5) {
  [0]=>
  string(3) "foo"
  [1]=>
  string(3) "bar"
  [2]=>
  string(3) "dim"
  [3]=>
  string(3) "sum"
  [4]=>
  string(4) "blah"
}
nikc
This worked for me.
Prashant
+4  A: 
preg_split('/[,;\ \n]+/', $_tags);

or if using php > 5.2.4

preg_split('/[,;\ \v]+/', $_tags);

or

preg_split('/[,;\s]+/', $_tags);
stefita