nickf
nickf

Reputation: 546075

How to double all line breaks?

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.

Upvotes: 3

Views: 338

Answers (2)

St. John Johnson
St. John Johnson

Reputation: 6660

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.

Upvotes: 1

Kalium
Kalium

Reputation: 4682

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.

Upvotes: 1

Related Questions