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 rn
as the line-break, and PHP’s nl2br() function can convert the rn
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 rn
.
What am I missing here?
Advertisement
Answer
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 rn
.
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);