0

I'm receiving data from a MSSQL database with PHP (using PDO sqlsrv driver).

I need to split the string by new line.

In the DB the string looks like this:

the string in the database

I tried the following with no success:

$text = explode("\n\n",$text);
$text = preg_split("/\n\n/",$text);

Or

$text = explode("\n",$text);
$text = preg_split("/\n/",$text);

When i debug the code and when i print the text to the browser the string appears as one line.

text echoed to the broswer

But if i inspect the element in chrome, it is displayed exactly like in the database:

text inspected with chrome

I copied the text from the inspector to a regex tester and the newlines were found successfully! But the "echoed" text was in one line.

Alex Leibovich
  • 315
  • 1
  • 16

1 Answers1

2

Try replacing new line character with comma (,) or something other special char.

preg_replace("/((\r?\n)|(\r\n?))/", ',', $input);

Then try to explode like this explode(',', $input);

or if result set is file go through this post

explode(PHP_EOL, $input);

https://stackoverflow.com/a/29471912/4895810

Jeevan
  • 317
  • 4
  • 18
  • Thanks, ended up using this method. Whats wierd that that `preg_split("/((\r?\n)|(\r\n?))/", $text);` just didn't work! Oh well... – Alex Leibovich Jun 06 '17 at 12:52