用某种空格分割字符串

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.

You can use preg_split to split your string:

<?php
$output = 'text
&NewLine;
&NewLine;more text
&NewLine;
&NewLine;ja';

$explode = preg_split('/(&NewLine;|(
||
))+/', $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 &NewLine;:

$output = 'text
&NewLine;
&NewLine;more text
&NewLine;
&NewLine;ja
&NewLine;nein';

$explode = preg_split('/(
||
)*(&NewLine;(
||
)*){2}/', $output, -1, PREG_SPLIT_NO_EMPTY);

demo: https://ideone.com/0txh5O

Adding one line in your code $output =str_replace(" ","",$output ); to combine all string in one line so that it would like your first example.

$output =str_replace("
","",$output );
$explode = explode('&NewLine;&NewLine;', $output);
print_r($explode);

Live demo : https://eval.in/887371

$explode = preg_split('/&NewLine;\s*&NewLine;/', $output);

normalize your string by removing all new line like chars:

$output = trim(preg_replace('/\s+/', '',$output));

then explode it.

As User2486 says, the problem is that there are some hidden characters you are not watching like and

In your first example you have

'text&NewLine;&NewLine;more text&NewLine;&NewLine;ja'

and in the second

'text
&NewLine;
&NewLine;more text
&NewLine;
&NewLine;ja'