Tutorial :How to double all line breaks?



Question:

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.


Solution:1

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.


Solution:2

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »