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.

Marin Sagovac
  • 3,932
  • 5
  • 23
  • 53

1 Answers1

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

Toto
  • 89,455
  • 62
  • 89
  • 125
  • 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! – Marin Sagovac Sep 09 '13 at 16:43