6

For an example I have tons of regex as simple I wrote:

php > var_dump( preg_split('/[:reg\s{}]+/', ':reg{/^[a-zA-Z]*$/}') );
array(3) {
  [0] =>
  string(0) ""
  [1] =>
  string(13) "/^[a-zA-Z]*$/"
  [2] =>
  string(0) ""
}

I want remove empty arrays [0] => string(0) "" for example a result should be:

php > var_dump( preg_split('????', ':reg{/^[a-zA-Z]*$/}') );
array(3) {
  [0] =>
  string(13) "/^[a-zA-Z]*$/"
}

I know array_filter() function when remove empty arrays, but I want using regular expression only.

1
  • Because I have a tons of regex which I'm writing, so I want to make a time shorter. Sep 9, 2013 at 16:35

1 Answer 1

8

Just use the flag PREG_SPLIT_NO_EMPTY:

preg_split('/[:reg\s{}]+/', ':reg{/^[a-zA-Z]*$/}', -1, PREG_SPLIT_NO_EMPTY);

See the doc

1
  • 2
    Okay, "-1" for no limit strings, and PREG_SPLIT_NO_EMPTY If this flag is set, only non-empty pieces will be returned by. Thank you! Sep 9, 2013 at 16:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.