4

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 ) 
5
  • have you try it with only one \n? Commented Sep 9, 2011 at 22:54
  • 1
    Try "\r\n\r\n" and see if that works. Commented Sep 9, 2011 at 22:55
  • 1
    probably, \r\n\r\n instead of \n\n Commented Sep 9, 2011 at 22:57
  • @David Laberge ofcours i did i got Array ( [0] => [1] => Hello, [2] => [3] => Just test lines [4] => Second test lines [5] => ) Commented Sep 9, 2011 at 23:00
  • cdhowie , lAbstractDownvoteFactory <-- thanks guys it's work fine Commented Sep 9, 2011 at 23:01

2 Answers 2

12

This should do the trick:

$string = '
Hello,

Just test lines
Second test lines
';

$data  = preg_split("#\n\s*\n#Uis", $string);
print_r($data);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, its can be used when you take info from linux servers, :) thanks a lot agin.
In case of using PHPStorm, this is passing the code parse error validation: '/\n\s*\n/Uis'.
0

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].",";

1 Comment

Trim isn't ideal when the spaces could be significative.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.