1

Assuming that $text = ea\r\ndad\r\n\r\nedade

My first code:

$text = explode("\r\n",$text);

My new code

function splitNewLine($text) {
    $code=preg_replace('/\n$/','',preg_replace('/^\n/','',preg_replace('/[\r\n]+/',"\n",$text)));
    return explode("\n",$code);
}
$text = splitNewLine($text);

In both situations, $text ends up like this:

Array
(
    [0] => ea\r\ndad\r\n\r\nedade
)

And I really can't figure why... This is about it, no more code missing and yet it won't work. Any idea on why this happens?

Fane
  • 1,978
  • 8
  • 30
  • 58
  • 1
    `preg_split('/\r?\n/', $str, -1, PREG_SPLIT_NO_EMPTY);` – Sverri M. Olsen Dec 05 '15 at 11:34
  • @SverriM.Olsen Nope, same. This is odd AF – Fane Dec 05 '15 at 11:42
  • Show us the code that you are using then. http://sandbox.onlinephpfunctions.com/code/e013edc2b051715b03fa60b8ebc2fb108d787b63 – Sverri M. Olsen Dec 05 '15 at 11:46
  • Probably you have ``$text = 'ea\r\ndad\r\n\r\nedade'``. So ``\n`` is not actually a line break. – Alexander Mikhalchenko Dec 05 '15 at 12:01
  • Even the explode solution just works, http://sandbox.onlinephpfunctions.com/code/a09631e4f98b0dddfee6bc675ac30e49662dbf4d so my guess is that something else is going wrong. Note that "\n" is different from '\n' so maybe that is something to look for? – l.renkema Dec 05 '15 at 12:03
  • @AlexanderM. That seems to be about it. Thank you very much :) – Fane Dec 05 '15 at 16:14
  • As @AlexanderM. pointed out, [" " and ' ' are different in PHP](http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – Fane Dec 05 '15 at 16:15
  • @Fane you're welcome :) – Alexander Mikhalchenko Dec 05 '15 at 17:57
  • @AlexanderM. Hi again alexander, sorry to bother you but would you mind checking this question of mine? http://stackoverflow.com/questions/34107677/how-to-make-sure-string-has-double-quotes?noredirect=1# It is related to this one and I was hoping you could help me... tyvm :)) – Fane Dec 05 '15 at 20:36

1 Answers1

1

The best is to use PHP_EOL

$temp = explode(PHP_EOL, $text);

OR

$temp = preg_split('/\r\n|\r|\n/', $text);

Hope it helps you

Jigar
  • 44
  • 4