Problems with Adding Breaks to User Input Data - php

I am making a textarea, the value of which should be inserted into a database but the user input can contain new lines and the output should also contain new lines.
I use nl2br($message) to accomplish that but if the user inputs something like:
<ul>
<li>hi</li>
<li>nice to meet you</li>
<li>bye</li>
</ul>
it gives way too many spaces between each text because the <li> tag itself also contains some kind of break, so that means 2 breaks.
My question is: how can I avoid this, or which other function could I use to save the breaks of my user input?

if you are using textarea, it's better not to allow html tags to insert.. use php strip_tags() function for removing the html tags before inserting to database..

Related

PHP output string and maintain spacing [duplicate]

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

PHP does not ignore html tags

I have input box in which user entered the string like
"/> <img src=xxx onError=alert('test is here')
but at the time of I have used strip_tags function before saving the value into the database. It igoners the image tag but the string "/> is saved in the database as it is.
How can I overcome with this.
To be honest here, there's not one go to solution unfortunately.
The strip_tags function works good on well formatted HTML and you example is not a valid one.
One of your options is to write a custom code that "cleans" the input depending on its nature. For example, if the input should collect someones age, strip anything that's not a digit. You can do the same for names, phones, etc. etc.
Of course, we as a developers, can't foreseen all possible non-sense that an user can enter (on purpose or not) and sometimes we end up with such data in the DB. That's why it's always a good idea to escape data before printing it in the HTML. All of the frameworks and template engines out there are already doing it for you. If you're not using a framework you can use htmlentities function - http://php.net/manual/en/function.htmlentities.php.
The htmlentities would make any HTML reserved characters save and won't break you page. For example:
htmlentities("/> <img src=xxx onError=alert('test is here')");
would result in:
/> <img src=xxx onError=alert('test is here')
And once rendered via the browser that would look like:

insert user input into mysql database as styled by the user (with line breaks)

I am looking to insert a user's input from a textarea into a mysql database. As of now, if the user types the following:
"Hello.
hello."
It will be inserted into the database without the line breaks. How can I fix this?
You are probably using phpMyAdmin to tell that the line breaks aren't there. If that's the case, then it is lying to you. phpMyAdmin displays the contents as HTML, and line breaks are transformed to a single white space.
Even if you're just outputting the value and not using phpMyAdmin, your HTML will still collapse the white space. To preserve the format, output it between <pre> tags or in a span with css style white-space: pre
Actually the line breaks are inserted in the database, you just can't see them, because they are "\r\n", this are Line Feed and Carriage Return, read more about them here
I believe that you should store the data like this: no conversion made, when you write the content to your HTML, you simply have to do an nl2br($content).
And it's also easy to edit, because when outputting that content to an textarea tag, it automatically recognizes the "\r\n"
If using PHP, i suggest using $message = nl2br($message);
This will turn line breaks into br's for you.

Why does PHP echo'd text lose its formatting?

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

Problem displaying the mysql content in Paragraphs

I insert questions(which might be a few paragraphs) in a sql table using php and than i diplay them on a webpage.
but when i display the question it loses its formatting. I mean it will just show the whole question in one paragraph, even thou there were many paragraphs before.
<td width=\"700px\" bgcolor=\"#EAD57F\"><font color=\"#4A2A0B\">Question :</font><font color=\"#5E450B\">".$row2['Question']."</font></td>
$row2['Question'] --> is my question that i am getting from my sql table by running the SELECT query.
So if i post something like :
a
s
d
f
into my input box.
the output looks like : asdf
How should i resolve this?
Best
Zeeshan
You probably save your paragraphs separated by a "new line" character. To translate that in HTML check the nl2br PHP function (in HTML new line is the <br /> tag).
Are you storing them as plain text, or do they contain HTML tags? If they are stored as plain text you should put them in a <pre> tag or something equivalent in order to preserve the spacing. Alternatively, you could do the encoding into HTML, putting in <p> tags and such where necessary, but that is complicated and easy to get wrong.

Categories