I need to parse this string:
$str = "
ABC
DEF
GHI
JKL" ;
into this array:
$arr = [
"
",
"ABC
",
"
",
"DEF
",
"GHI
",
"
",
"JKL"
] ;
I tried many combinations, but no luck:
$arr = preg_match_all("/[^
]+[
]+/",$str,$out) ;
What Regex can handle that?
This pattern does the job:
preg_match_all('~
+|.+
?~', $str, $matches);
As an aside, preg_match_all
returns the number of matches or false
, the matches are stored in the third parameter.