4

I am trying to have lyrics to a song submitted into a text area. So, this needs to keep the line-breaks when I output. I am aware of the fact that browsers standard should use \r\n as the line-break, and PHP's nl2br() function can convert the \r\n into <br>tags.

I am also aware of the fact that textarea inputs require wrap="hard" in order to pass the line-breaks, otherwise the default of wrap="soft" will ignore, and that cols="" must be specified when using wrap="hard". I have attempted to accomplish this with the html below:

<textarea wrap="hard" name="reading_text" cols="450" rows="6" maxlength="5000"></textarea>

However, the POST array does not show any line breaks. I enter this into the textarea:

Line one of the lyrics

That were entered into the textarea

Alas to my dismay

Red eyes and fire and signs

Because the line-breaks are not in the POST data

I want to make a ray of sunshine and never leave home

I get this in the POST array:

array(5) { ["reading_text"]=> string(216) "Line one of the lyrics That were entered into the textarea Alas to my dismay Red eyes and fire and signs Because the line-breaks are not in the POST data" ["add_reading"]=> string(3) "Add" }

To make matters more confusing for me I tried to look at W3 Schools demo (https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_textarea_wrap). However, I cannot seem to even make that example work. When I enter many line breaks into the textarea, the text comes out with no \r\n.

What am I missing here?

raw-bin hood
  • 5,839
  • 6
  • 31
  • 45
  • 1
    take a look https://stackoverflow.com/questions/498461/how-to-save-user-entered-line-breaks-from-a-textarea-to-a-database – dianeryanto Mar 05 '19 at 02:12
  • I read that, but it is just the same general rules. I can't even seem to get data to the server with '\r\n' in it. If I have a textarea and use wrap="hard" in a form and use POST, then shouldn't the POST element have '\r\n' in the string? My string has no \r\n. – raw-bin hood Mar 05 '19 at 02:29

3 Answers3

3

Regarding the wrap attribute, you do not need it for what you are trying to achieve. As described in the HTML 5.2 specification, line breaks within the text are preserved when using either the soft or hard attribute options.

The hard option is only required when you want to limit the length of each line of the input. Use this option to force additional line breaks where-ever your input naturally wraps (i.e., where-ever the line is longer than the value set in cols).

For example, if I have a textarea that is set to cols="20" wrap="hard" and I enter the text:

This is a long string that will exceed the number of cols in this textarea

Then submitted value will become:

This is a long
string that will
exceed the number of
cols in this
textarea

If I have understood correctly, you only want to preserve the line breaks that were intentionally entered by the user; these will be captured using the default soft option.

Regarding the submitted text, please note that the escape characters \r\n will not be seen in the posted value, the newline characters are still in the same interpreted form as they were in when you hit the "enter" key on your keyboard.

Even though you cannot see the escape characters, the newlines do exist and can still be found and manipulated by PHP. As you suggested, nl2br can be used to convert the new lines into HTML break elements.

If you actually need to convert the newlines into their corresponding escape characters, perhaps before storing the value, then there are a number of ways to manipulate the string. A few examples:

$escaped_newlines = str_replace("\r\n", '\r\n', $_POST['reading_text']);
$escaped_newlines = preg_replace('/\r\n/', '\r\n', $_POST['reading_text']);

(For further reading, there's a useful S.O. answer about double/single quotes and interpreting newlines)

Finally, how are you currently viewing the contents of $_POST? If you are outputting the contents of the var_dump directly into the HTML, then you will need to wrap the output in <pre></pre> tags in order to see the line breaks in the resulting HTML. Better still, output to a log file so that you do not need to consider such rendering issues.

  • That's a great explanation. Now I understand "hard" and "soft" attributes exactly and also, how the line-breaks are interpreted in the browser DOM and how to store or convert the data. – raw-bin hood Mar 06 '19 at 00:46
  • Also, thanks for the like to the HTML 5 specification. – raw-bin hood Mar 06 '19 at 00:50
0

You could pass the textarea text as part of the querystring. This will force the entire string to be url encoded like this:

%0AGray%2C%20quiet%20and%20tired%20and%20mean%0A%0APicking%20at%20a%20worried%20seam%0A%0AI%20try%20to%20make%20you%20mad%20at%20me%20over%20the%20phone.%0A%0ARed%20eyes%20and%20fire%20and%20signs%0A%0AI%27m%20taken%20by%20a%20nursery%20rhyme%0A%0AI%20want%20to%20make%20a%20ray%20of%20sunshine%20and%20never%20leave%20home%0A

then just decode the string on the receiving page with urldecode(). This should keep the formatting and display it correctly.

http://php.net/manual/en/function.urldecode.php

https://www.tools4noobs.com/online_php_functions/urldecode/

I suppose you could just also urlencode() the textarea text before POSTING and urldecode it on the other end.

0

Here is what finally solved the issue: I was using PHP's filter FILTER_FLAG_STRIP_LOW (http://php.net/manual/en/filter.filters.flags.php) on the string before you output or store to the database you will remove the newline characters \r\n .

To fix this issue you can use the FILTER_FLAG_ENCODE_LOW instead of FILTER_FLAG_STRIP_LOW as such:

$input = filter_var($input, FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW);
raw-bin hood
  • 5,839
  • 6
  • 31
  • 45