This qeustion seems simple but i loss lot of my time playing with it.
My requirement is simple i want a text area that supports line break i.e if a user enters(line break) and again types some text.When this value is stored in database and if i want to display that text on some page than it should exactly display with same formatting as entered.
Also the text area should support some special characters like this text mofrém.Here these characters are displayed as boxes.
Using tinymce is an option i guess, but is there any simpler solution? Can these text be manipulated using php?
Any help would be appreciated.
Found a similar question that has been answered:
Preserve Line Breaks From TextArea When Writing To MySQL
Posted by user Zuul
Two solutions for this:
PHP function nl2br():
e.g.,
echo nl2br("This\r\nis\n\ra\nstring\r");
// will output
This<br />
is<br />
a<br />
string<br />
Wrap the input in <pre></pre> tags.
See: W3C Wiki - HTML/Elements/pre
Edit:
In reply to comment. To store/encode special characters use:
nl2br(htmlspecialchars($text));
Then to decode again special characters:
htmlspecialchars_decode($text);
Reference:
http://php.net/manual/en/function.htmlspecialchars.php
http://php.net/manual/en/function.htmlspecialchars-decode.php
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 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:
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 am trying to display comments on a page and am having some trouble.
There are essentially two different types of comments I am trying to handle:
(1) The XSS type.. e.g. <script type="text/javascript">alert('hi')</script>. This is handled fairly easily by escaping it before it gets into the database and then running stripslashes and htmlentities on it.
(2) The comment with <br> breaks in it. When the data is stored into the database, I am running nl2br on it so the data looks like hi<br>hello<br><br>etc. However, when I display this comment, the <br>s do not turn into page breaks like I want them to.
Any idea what to do? I should note that turning off htmlentities fixes the second type, but the first type then is executed as pure html and displays an alert dialog.
Thanks,
Phil
If you want to remove unwanted tags you can try strip_tags. It supports allowable_tags so you can specify any tags that you don't want to be stripped. A sample from the manual:
// Allow <p> and <a>
// you can add <br> if you want it not stripped
echo strip_tags($text, '<p><a>');
So after you've converted all \n to be line breaks you dont have to worry about it being stripped. May not be what you want but hope it gives an idea.
One method: Replace <br> with a placeholder, like \n. Then do htmlentities to clean up html code. Finally, replace \n back with <br> to recover the line breaks.
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.