Emad doom
Emad doom

Reputation: 87

Explode empty line

I am trying to explode empty line.

Here is my string variable:

$string = '
Hello,

Just test lines
Second test lines
';

I want these results:

$OUTPUT[0]='HELLO,';
$OUTPUT[1]='Just test lines
Second test lines';

My code:

$a = explode("\n\n",$string);
print_r($a);

My field results:

Array ( [0] => Hello, Just test lines Second test lines ) 

Upvotes: 4

Views: 4117

Answers (2)

Rooster
Rooster

Reputation: 10077

throw a trim around your string, then explode it with the comma, then add the comma back on to the first element.

$arraything = explode(',', trim($string));
$arraything[0] = $arraything[0].",";

Upvotes: 0

Kęstutis
Kęstutis

Reputation: 545

This should do the trick:

$string = '
Hello,

Just test lines
Second test lines
';

$data  = preg_split("#\n\s*\n#Uis", $string);
print_r($data);

Upvotes: 12

Related Questions