2

I have the following text:

$test = 'Test This is first line

Test:123

This is Test';

I want to explode this string to an array of paragraphs. I wrote the following code but it is not working:

$array = explode('\n\n', $test);

Any idea what I'm missing here?

HamZa
  • 13,530
  • 11
  • 51
  • 70
Deep123
  • 381
  • 1
  • 4
  • 17
  • Have you tried `\r\n` or maybe `PHP_EOL` constant? – naththedeveloper Jul 16 '13 at 09:53
  • does it work with $test = 'Test This is first line\n\nTest:123\n\nThis is Test'; ? I'm not sure about this, but maybe you need to explicetly include the linebreaks when defining your string as a variable. – astrognocci Jul 16 '13 at 09:54

6 Answers6

7

You might be on Windows which uses \r\n instead of \n. You could use a regex to make it universal with preg_split():

$array = preg_split('#(\r\n?|\n)+#', $test);

Pattern explanation:

  • ( : start matching group 1
  • \r\n?|\n : match \r\n, \r or \n
  • ) : end matching group 1
  • + : repeat one or more times

If you want to split by 2 newlines, then replace + by {2,}.


Update: you might use:

$array = preg_split('#\R+#', $test);

This extensive answer covers the meaning of \R. Note that this is only supported in PCRE/perl. So in a sense, it's less cross-flavour compatible.

Community
  • 1
  • 1
HamZa
  • 13,530
  • 11
  • 51
  • 70
  • `preg_match_all('/.+/',$test, $test)` still is a lot easier IMHO, and does what the OP wants – Elias Van Ootegem Jul 16 '13 at 10:04
  • @EliasVanOotegem Well now that you say it ... I was focused on the "split" idea :p The split solution has got more control if we want to split by 2 newlines or plus :) – HamZa Jul 16 '13 at 10:12
  • 1
    Fair point. Since the OP specified paragraphs as delimiters, I'd have to say your approach is the one to go for – Elias Van Ootegem Jul 16 '13 at 10:43
3

Your code

$array = explode('\n\n', $test);

should have \n\n enclosed in double quotes:

$array = explode("\n\n", $test);

Using single quotes, it looks through the variable $test for a literal \n\n. With double quotes, it looks for the evaluated values of \n\n which are two carriage returns.

Also, note that the end of line depends on the host operating system. Windows uses \r\n instead of \n. You can get the end of line for the operating system by using the predefined constant PHP_EOL.

BLaZuRE
  • 2,295
  • 2
  • 23
  • 40
2

Try double quotes

$array = explode("\n\n", $test);
HamZa
  • 13,530
  • 11
  • 51
  • 70
nickle
  • 3,806
  • 1
  • 11
  • 11
1

The easiest way to get this text into an array like you describe would be:

preg_match_all('/.+/',$string, $array);

Since /./ matches any char, except for line terminators, and the + is greedy, it'll match as many chars as possible, until a new-line is encountered.
Using preg_match_all ensures this is repeated for each line, too. When I tried this, the output looked like this:

array (
  0 => 
  array (
    0 => '$test = \'Test This is first line',
    1 => 'Test:123',
    2 => 'This is Test\';',
  ),
)

Also note that line-feeds are different, depending on the environment (\n for *NIX systems, compared to \r\n for windows, or in some cases a simple \r). Perhaps you might want to try explode(PHP_EOL, $text);, too

AlphaMycelium
  • 427
  • 3
  • 17
Elias Van Ootegem
  • 67,812
  • 9
  • 101
  • 138
0

did you have try this ?

$array = explode("\n", $test);
Amir Habibzadeh
  • 380
  • 1
  • 11
0

You need to use double quotes in your code, such that the \n\n is actually evaluated as two lines. Look below:

'Paragraph 1\n\nParagraph 2' =

Paragraph 1\n\nParagraph 2

Whereas:

"Paragraph 1\n\nParagraph 2" =

Paragraph 1

Paragraph 2

Also, Windows systems use \r\n\r\n instead of \n\n. You can detect which line endings the system is using with:

PHP_EOL

So, your final code would be:

$paragraphs = explode(PHP_EOL, $text);
AlphaMycelium
  • 427
  • 3
  • 17