How to preserve line breaks in textarea? - php

I went through similar questions on this issue but couldn't find a solution.
I have a simple textarea in which text will be pasted or entered directly.
How do I preserve those line breaks?
Right now line breaks will just be ignored, but when I print_r($_POST) and inspect the output in the Chrome Developer Console it still has the original text breaks.
How can I display those when showing the submitted data?

Right now line breaks will just be ignored
No, it is still there. The key you miss is that what you enter goes as ASCII code LF (0xA) but this makes no effect in HTML unless the text is displayed as preformatted block (<pre> ... </pre>). If not, then line break is represented bytag so what you need to do if you want to display entered text is to convertLFs to(you can usenl2br()function to do that) or use` block.

You can use nl2br() function:
Inserts HTML line breaks before all newlines in a string
echo(nl2br($_POST));
More information at http://php.net/manual/en/function.nl2br.php.

As Marcin noted, use nl2br() when displaying your data as instructed in this answer: https://stackoverflow.com/a/6480671/6206601

Related

Why do I get \n instead of a new line getting data from AJAX?

I have a problem: so I have a data thats coming out of the database and stored in a text area. When the user changes the data in the text area, the content is sent to javascript and via AJAX (POST to a PHP script) the database is updated. This works fine until the user starts adding newlines. Then javascript transforms this into a \n-character and thus it gets stored in the database as \n.
What I want is to have actual newlines in my database and not the \n newline-characters. Is there any way that I can use php to replace the \n with an actual newline (NOT a br)? I have tried altering the database field after the edit with the char(10), but for some reason this is not working in the script except when I do it manually in phpmyadmin?
When editting with a full php request, the newline in a text area is correctly stored as a char(10) in mysql, not as \n.
Anyone got a clue?
Store it as it is but escape first with real_escape_string
real_escape_string converts what is a newline into the 4 character string '\n\r'
$text = $mysqli->real_escape_string($text);
Use [nl2br][1] function to replace /n with newline
Insert line breaks where newlines (\n) occur in the string:
<?php
echo nl2br("One line.\nAnother line.");
?>
The browser output of the code above will be:
One line.
Another line.

How do i enable line breaks in php?

I am making a comments system in which i can accept user input with line breaks.
I don't want to show the \n or \r thing
Please help me with this
nl2br($string);
is fast and easy
They are enabled by default. If you are outputting the text to a web browser, make sure to use nl2br or the white-space attribute in CSS.
using preg_replace
simply replace it
preg_replace('/\n/'," ",$str);
\n should do the trick.
if you are trying to output a textarea, then use nl2br();
also:-
If you are trying to format your HTML source, you should use the constant PHP_EOL. The main reason being that on windows machines the EOL is \r\n and on UNIX machines it is \n. With a properly installed LAMP set up just use PHP_EOL like so.
$html.="<p>This is my HTML</p>" . PHP_EOL;
Line breaks will be stored just like any other character.
\n is an escape code that allows you to explicitly insert a line break into a string, but I don't think that it's relevant here.
The issue you're actually facing is that HTML does not impart any visual meaning to a line break. Line breaks within HTML code do not, under normal circumstances, equate to a line break on the screen.
One way to render a line break in HTML is to use a line break tag, or <br>.
In PHP, you can automatically convert line breaks to <br> with the nl2br function. Applying this to your comment text when you output it into HTML will enable you and other visitors to see the line break visually.

What's the trick to display a text from MySql/PHP back to its original HTML layout?

It still keeps the original text layout (I mean the spacing, offsets, new line, paragraphs) while the text fragment is stored in MySql ('text' type) field - I can tell when I peer into it in my DB browser (Adminer:)
but it gets lost when I output it from the DB: it becomes a single line string of my text characters. How can one restore it its original layout?
I've tried to reshape the text fragment using the PHP nl2br() function with some success:
it brought back the newline breaks, but the text words positioning is not kept, everything
shifts to the left.
Thanks in advance for a good idea.
If you've got multiple spaces and things like that. e.g. for code. Then trying using the pre tag.
http://htmldog.com/reference/htmltags/pre
http://reference.sitepoint.com/html/pre
The html_entity_decode() function converts HTML entities to characters.
The syntax is:
html_entity_decode(string, [quotestyle], [character-set]);
You can refer example2.

Textarea Adding Space To Beginning of Text?

I'm having a strange problem in that I have php inserting text into a <textarea> and the <textarea> is adding one white space to the top of my text.
I created an example page to display the problem... here it the code behind the page.
<textarea style="width:600px;height:100px;"><?php get_film_info('main description'); ?></textarea>
<br>
<textarea id="mainDescription style="width:600px;height:100px;">Text just typed in</textarea>
<br>
<?php get_film_info('main description'); ?>
You can see that without the <textarea> tag, the text does not include the indent. My database also reflects no indent, as well as the php output outside of the <textarea>...
Any clue what could be going on?
the sample page
Edit: You were all right, of course I didn't bother checking the source code of the output file. Turns out when I was adding the data (via ajax) I was sending my data like var data = '&main_description= ' + mainDescription. Notice the space between the "=" and the "+".
Thank you all for your input, gotta just give the check mark to the guy on the top of the list.
Try this:
trim(get_film_info('main description'));
Your text has space at beginning!
I don't know what function 'get_film_info' returns but it returns with space!
There's definitely a space in the beginning and one at the end, as can be seen in the page source. Perhaps get_film_info() is inadvertently injecting them.
There is a heading space in the return value of get_form_info(). Check the value of 'main description' in your database (or whereever it is being stored). If there isn't a heading space in the value itself, then get_film_inflo() is to blame.
A space does exist. Outside of the textarea, the browser does not interpret them, because a \n means nothing (it is interpreted in the source code only) in just regular text form. However, a \n inside a textarea represents a line break and is being interpreted as such.
To solve the problem you can always trim the value before outputting it.

PHP form removing line breaks

I use a php form processor script that works fine. Except when users submit text in a multi-line text field, any line breaks or new lines are stripped out of the resulting string variable that is passed on. This often makes it unreadable by whoever receives the form results.
I'm no php expert but am sure the answer lies in the code that is stripping characters. What I'm unsure of is if I stop it stripping characters will this result in a security risk?
The strip array reads:
array('*', '|', '>', '<', '/', '\\\', '\"', 'Bcc', 'BCC', 'bcc');
What can I change here to retain the line breaks?
Thanks in advance for any help.
If your problem is with the submitted string then this means that the submitted string did not contain any line breaks or newline chars.
In one occassion i looked up the wrap="(hard|physical)" attribute on the text area. Some values of this attribute force the textarea to maintain line-breaks in the user text.
Did you try using nl2br($text) on the submitted text;
I believe you have an issue at the rendering phase. Have you tried:
echo nl2br($text);
Where $text is the text you're talking about.
Nothing there is stripping line breaks.
It's more likely that you're not doing anything on the display side to make the line breaks display. You're outputting HTML, and to HTML a line break is just more whitespace. You'll probably benefit from applying nl2br().

Categories