Jump to content

converting multiple lines breaks into singles


thecommonthread

Recommended Posts

So I have this textarea in an HTML form that I need to convert the contents of from having multiple line breaks into only one line break for each.

 

Example: I want to convert the following:

I went to the store.

I had a good time.



I ran into a friend.

 

Into this:

I went to the store.
I had a good time.
I ran into a friend.

 

Any help would be super appreciated.

Link to comment
Share on other sites

You may be able to do it like this:

 

$text = preg_replace("|\n+|", "\n", $text);

 

It may not work if the line character is not just "\n".  If it doesn't work, try "\r\n" in place of both of the "\n".

 

Another approach is to use explode() or preg_split() to put the lines in an array, then remove empty lines, then join() them together again.

Link to comment
Share on other sites

I tried your first method and couldn't make it work unfortunately.

 

If I used the explode() method, how would I remove empty lines in the array?  Maybe I'm not understanding this method completely.

can you show how you tried it, with code

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.