A user post a comment on a forum using a textarea, he presses enter to format his text which means line breaks, how can I convert those line breaks to html breaks when inserting its message on the database so the message is echoed with the line breaks formatting?
There's a function called nl2br(). Use it when echoing data, not when inserting to database. You don't want to display those <br/> tags when the user opens their post in textarea for editing, do you?
Use nl2br()
It will convert new lines to <br>, so html will understand it.
Use nl2br() function. It replaces all newlines within a string with html br tags.
For example (taken from php manpage):
<?php
echo nl2br("foo isn't\n bar");
?>
Outputs:
foo isn't<br />
bar
The best practice is to use it when outputting data and store the data in the original ofrmat (with standard newlines).
You mean this?:
$html_text = nl2br($text);
Related
Any ideas why formatted text from DB, when echo-ed out in php loses its formatting, i.e. no new lines? Thanks!
Use nl2br().
New lines are ignored by browser. That's why you see all text without line breaks. nl2br() converts new lines to <br /> tags that are displayed as new lines in browsers.
If you want to display your text in <textarea>, you don't need to convert all new lines to <br />. Anyway, if you do it... you will see "<br />"s as text in new lines places.
Because there are no html tags for formatting!
Try the nl2br function.
You could try add nl2br() function...
something like this: echo nl2br($your_text_variable);
It should work ;-)
The reason
This is the default behavior for all user agents. If you look at the page source, you'll see that your text has the same formatting like the one in the database (or textarea).
The reason of your confusion is probably that you once see the text in the <textarea> tag, which displays preformatted text, does not interpret the tags, and in the other case the text is interpreted (whitespace is not important in this case).
The browsers don't display new lines, unless specifically asked for - using <br> tag or any block level tags.
No tags == no new lines.
The fix
If you store preformatted text in the database,
you should wrap the output in the <pre> tag.
You may want to convert the formatting characters to the HTML tags you need using set of functions like nl2br, str_replace etc.
You may also correct your structure to store the HTML in the database instead of just plain text (however markup looks like a better solution).
See similar question:
How do I keep whitespace formatting using PHP/HTML?
The difference between the two images you show is that one has the text in a <textarea></textarea> and the other does not ... if you want 1:1: <textarea><?php echo $yourVariable;?></textarea>
It does output what you say to output. If the text is pre-formatted, put it inside the HTML <pre></pre> tag in your output script.
This should be helpful in answering.
How do I keep whitespace formatting using PHP/HTML?enter link description here
Set up a string preprocessing code for both input to database and output to display page
I'm having a problem on displaying values that were from a textarea.
I wanted users to enter some text in my textarea at the same time they may enter a new line(they will hit enter) then after that one, I want to get the value of that textarea and display it in my <span> tag.
The problem here is that new lines aren't displaying in my span tag, I don't know what happen.
For example a user has type something like this in in my text area:
Hello World!
How Are you?!
The problem is that in my span tag, it displays without new line, so the outcome is like this:
Hello World! How Are you?!
I don't want to use some WYSIWYG application just to allow new lines in my span. I believe there's a way for this one.
Please guide me on this one. Your help wouldbe greatly appreciated!
Thanks! :)
Textarea's make \n's, what you want is the html equivalent <br /> so use this string replace to replace all the \n's to <br />'s and you're golden.
In javascript it would be
str = str.replace(/\n/g, '<br />');
In php it would be
$str = nl2br($str);
It is normal for the browser to compress multiple white-space characters and newline characters back to a single space. Inserting <br> elements where you want line breaks is the usual method.
Your question is tagged with both PHP and JavaScript, but you don't need both for this task. If you're displaying data from a database then it'd be best to do the processing in PHP code. For dynamic page updates in the browser you'd use JS.
In PHP use nl2br() when preparing the text that ends up in the span:
echo nl2br($strWithNewLines);
Or in JS:
var stringWithBrs = strWithNewlines.replace(/\n/g, '<br />')
Try this trick.
<pre><span id="wrapContent">
<?php echo $str; ?>
</span></pre>
I'm doing a web builder and I'm having a bit of trouble.
I want to have a textarea that you can enter text that you want to your TEXT element. This element I disabled the HTML when it´s later off previewed by simply putting a .innerText before posting preview. Though, I might just need the <br> tag.
I can use PHP or JS. Any ideas?
Use nl2br(htmlspecialchars($str)) when displaying the text. Note that the order of the function calls matters - first you escape all HTML in the string and then you convert linebreaks to HTML linebreaks.
When you are typing in a textarea, and the return key is pressed,
What actually goes on behind the scenes is this
Hi, \n There
which produces the following in the textarea.
Hi
There
Hence, what you would need to do is essentially change the \n (newline) to break tags.
http://php.net/manual/en/function.nl2br.php
Just use a plain textarea, afterword use a function like nl2br when you display it in your html page
nl2br(htmlspecialchars($string))
changes the line breaks to <br /> tags, so you can display them in html as seen in the textarea.
Any ideas why formatted text from DB, when echo-ed out in php loses its formatting, i.e. no new lines? Thanks!
Use nl2br().
New lines are ignored by browser. That's why you see all text without line breaks. nl2br() converts new lines to <br /> tags that are displayed as new lines in browsers.
If you want to display your text in <textarea>, you don't need to convert all new lines to <br />. Anyway, if you do it... you will see "<br />"s as text in new lines places.
Because there are no html tags for formatting!
Try the nl2br function.
You could try add nl2br() function...
something like this: echo nl2br($your_text_variable);
It should work ;-)
The reason
This is the default behavior for all user agents. If you look at the page source, you'll see that your text has the same formatting like the one in the database (or textarea).
The reason of your confusion is probably that you once see the text in the <textarea> tag, which displays preformatted text, does not interpret the tags, and in the other case the text is interpreted (whitespace is not important in this case).
The browsers don't display new lines, unless specifically asked for - using <br> tag or any block level tags.
No tags == no new lines.
The fix
If you store preformatted text in the database,
you should wrap the output in the <pre> tag.
You may want to convert the formatting characters to the HTML tags you need using set of functions like nl2br, str_replace etc.
You may also correct your structure to store the HTML in the database instead of just plain text (however markup looks like a better solution).
See similar question:
How do I keep whitespace formatting using PHP/HTML?
The difference between the two images you show is that one has the text in a <textarea></textarea> and the other does not ... if you want 1:1: <textarea><?php echo $yourVariable;?></textarea>
It does output what you say to output. If the text is pre-formatted, put it inside the HTML <pre></pre> tag in your output script.
This should be helpful in answering.
How do I keep whitespace formatting using PHP/HTML?enter link description here
Set up a string preprocessing code for both input to database and output to display page
How can I read the New-line so that when users fill in a text-area inside my form, and then hit submit, the text is displayed exactly the same way as they wrote it?
I should mention I read in the variables from the form with POST method in my php file.
Thanks
The newline character in PHP is presented by the string "\n".
If you want to reproduce the newlines of your users input in HTML, you have to convert every \n to a <br />. You could do that by hand or use the function nl2br in such a way:
echo nl2br($_POST['input']);
Take a look at the nl2br function.
Example from documentation:
<?php echo nl2br("foo isn't\n bar"); ?>
The above example will output:
foo isn't<br /> bar