Reputation: 25
I've got the following code:
$output2 = 'text

more text

ja';
$explode = explode('

', $output);
This works fine and the $explode
array prints the following:
Array
(
[0] => text
[1] => meer text
[2] => ja
)
However, the following code doesn't work and I don't know how to fix it:
$output = 'text



more text



ja';
$explode = explode('

', $output);
The $explode
array prints the following:
Array
(
[0] => text



 //more text



ja
)
This might seem like a weird question. But the first example is a test I made manually. But the second example is what is actually returned from the database.
Upvotes: 2
Views: 57
Reputation: 43564
You can use preg_split
to split your string:
<?php
$output = 'text



more text



ja';
$explode = preg_split('/(
|(\r\n|\r|\n))+/', $output, -1, PREG_SPLIT_NO_EMPTY);
demo: https://ideone.com/KU0v9t (ideone) or https://eval.in/887393 (eval.in)
The following solution to split on double 

:
$output = 'text



more text



ja

nein';
$explode = preg_split('/(\r\n|\r|\n)*(
(\r\n|\r|\n)*){2}/', $output, -1, PREG_SPLIT_NO_EMPTY);
Upvotes: 1
Reputation: 314
As User2486 says, the problem is that there are some hidden characters you are not watching like \n and \r
In your first example you have
'text

more text

ja'
and in the second
'text\r\n
\r\n
more text\r\n
\r\n
ja'
Upvotes: 0
Reputation: 799
normalize your string by removing all new line like chars:
$output = trim(preg_replace('/\s+/', '',$output));
then explode it.
Upvotes: 0
Reputation: 34914
Adding one line in your code $output =str_replace("\r\n","",$output );
to combine all string in one line so that it would like your first example.
$output =str_replace("\r\n","",$output );
$explode = explode('

', $output);
print_r($explode);
Live demo : https://eval.in/887371
Upvotes: 0