tags:

views:

118

answers:

2

Edit: The problem described below was just caused by a "feature" of my IDE, so there's actually nothing wrong with the regex. If you're interested in how to double line breaks, here's your question and answer in one neat package. :)

I want to change every line break in a string to be two line breaks:

"this is
an example
string"
// becomes:
"this is

an example

string"

However, it needs to take Unix/Windows line endings into account. I've written the code below, but it's not behaving itself.

$output = preg_replace("/(\r?\n)/", "$1$1", $input);

But it's not working. Changing the replacement string to this...

"$1 $1"

...makes it work, but then I have an unwanted space in between.

+1  A: 

That's interesting. I just tested your sample code on two different UNIX systems (Ubuntu and a FreeBSD box, for the record). In both cases, it worked exactly as you say you wish it to. So your platform or your configuration may be partially at fault.

Kalium
+1  A: 

Wait Wait. Are you just directly outputting that to the browser? Did you View Source? Returns are not shown in HTML. Try putting <pre> before and </pre> after, if you want to view the returns as line breaks.

St. John Johnson
it's not HTML. It's being output into a file.
nickf