0

How to exclude empty lines in pref_split.

example: https://www.phpliveregex.com/p/qRm#tab-preg-split

user3631047
  • 3,256
  • 5
  • 23
  • 34
  • Can `\r` appear multiple time in the middle of the text, otherwise `trim()` the text before split it. –  Feb 08 '19 at 04:07

1 Answers1

0

Use the PREG_SPLIT_NO_EMPTY flag:

$string = "Name|name
Last|f_name
";
print_r(preg_split('/\R/', $string, -1, PREG_SPLIT_NO_EMPTY));

Output:

Array (
    [0] => Name|name
    [1] => Last|f_name 
)

Demo on 3v4l.org

Nick
  • 138,499
  • 22
  • 57
  • 95