Convert line ending to <br>

This is more of a ruby question, but I have a textarea in a form, and
for later presentation I need to convert the line endings to
.

The value from the form is, for example “line of text\r\nmore text”.
I tried gsub(’\r\n’, “
”), but it doesn’t find the \r\n. How do I
specify those characters?

Thanks,
-George

On 8/29/07, George B. [email protected] wrote:

This is more of a ruby question, but I have a textarea in a form, and
for later presentation I need to convert the line endings to
.

The value from the form is, for example “line of text\r\nmore text”.
I tried gsub(’\r\n’, “
”), but it doesn’t find the \r\n. How do I
specify those characters?

You would need to use double quotes around \r\n, “\r\n”, or express it
as a regex, /\r\n/.

But see also the built-in simple_format helper.

George B. wrote:

The value from the form is, for example “line of text\r\nmore text”.
I tried gsub(’\r\n’, “
”), but it doesn’t find the \r\n. How do I
specify those characters?

gsub("\r\n", “
”)

Double-quote the \r\n.

On Aug 29, 2007, at 1:29 PM, Daniel W. wrote:

gsub("\r\n", “
”)

Double-quote the \r\n.

That was it. Thanks!

-George

Occasionally it will be \n, and not \r\n.

text.gsub("\n",
)

use this
http://webonrails.com/2006/08/07/converting-all-newline-characters-to-br-tag/

rails has its own simple_format function, it may meet your need.

simple_format(text)
Returns text transformed into HTML using very simple formatting rules
Surrounds paragraphs with <p> tags, and converts line breaks
into <br /> Two consecutive newlines(\n\n) are considered as a
paragraph, one newline (\n) is considered a linebreak, three or more
consecutive newlines are turned into two newlines

On Aug 29, 2007, at 5:32 PM, Ryan B. wrote:

Occasionally it will be \n, and not \r\n.

text.gsub("\n",
)

Thanks. I added that after the \r\n line. I figured one of them has
to work.
But then I tried out the simple_format that Bob S. suggested,
which seems to be a better way to do it.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs