Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove empty arrays from preg_split result?

Tags:

arrays

regex

php

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.

like image 672
Marin Sagovac Avatar asked Sep 09 '13 16:09

Marin Sagovac


1 Answers

Just use the flag PREG_SPLIT_NO_EMPTY:

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

See the doc

like image 127
Toto Avatar answered Nov 20 '22 15:11

Toto