help with text formatting from textarea
Moderator: General Moderators
help with text formatting from textarea
Hey Guys!
I got a question about php. I have a textarea where users can send me a feedback about my site. What I want to know is how do I keep the text formatting from the textarea so I can display it with the same format?
I got a question about php. I have a textarea where users can send me a feedback about my site. What I want to know is how do I keep the text formatting from the textarea so I can display it with the same format?
- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
feyd, I dont see htmlentities work for this script.
I got one doubt for a long time. if I use nl2br on a string and add <br />, will htmlentities turn html parts(<br>s) to html equivalents???
I got one doubt for a long time. if I use nl2br on a string and add <br />, will htmlentities turn html parts(<br>s) to html equivalents???
Code: Select all
<html>
<body>
<form name = 'frmText'action = '' method = 'post'>
<textarea rows = '12' cols ='60' name = 'taSomething' ><?php if (isset($_POST['taSomething'])) echo $_POST['taSomething']; ?></textarea>
<input type = 'submit' name = 'subText' value = 'submit' />
</form>
</body>
</html>
<?php
if (isset($_POST['subText'])){
echo "<textarea rows = '10' cols = '60'>".nl2br($_POST["taSomething"])."</textarea>";
echo "<br />".htmlentities($_POST["taSomething"])."<br >";
echo "<textarea rows = '10' cols = '60'>".htmlentities($_POST["taSomething"])."</textarea>";
echo "<textarea rows = '10' cols = '60'>".htmlentities(nl2br($_POST["taSomething"]))."</textarea>";
echo "<textarea rows = '10' cols = '60'>".strip_tags($_POST["taSomething"])."</textarea>";
echo "<textarea rows = '10' cols = '60'>".strip_tags(nl2br($_POST["taSomething"]), "<br><b>")."</textarea>";
}
?>- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
try the code at http://raghavan.100webcustomers.com/test.php
Code: Select all
<html>
<body>
<form name = 'frmText'action = '' method = 'post'>
<textarea rows = '12' cols ='60' name = 'taSomething' style="background-color:#000099; color:#FFFFFF; "><?php if (isset($_POST['taSomething'])) echo $_POST['taSomething']; ?></textarea>
<input type = 'submit' name = 'subText' value = 'submit' />
</form>
</body>
</html>
<?php
if (isset($_POST['subText'])){
echo "<textarea rows = '10' cols = '60'>".nl2br($_POST["taSomething"])."</textarea>";
echo "<br />".htmlentities($_POST["taSomething"])."<br >";
echo "<textarea rows = '10' cols = '60'>".htmlentities($_POST["taSomething"])."</textarea>";
echo "<textarea rows = '10' cols = '60'>".htmlentities(nl2br($_POST["taSomething"]))."</textarea>";
echo "<textarea rows = '10' cols = '60'>".nl2br(htmlentities($_POST["taSomething"]))."</textarea>";
echo "<textarea rows = '10' cols = '60'>".strip_tags($_POST["taSomething"])."</textarea>";
echo "<textarea rows = '10' cols = '60'>".strip_tags(nl2br($_POST["taSomething"]), "<br><b>")."</textarea>";
}
?>- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
the example from PHP manual
input
should output
but mine outputs as
ofcourse, i see a difference 
input
Code: Select all
A 'quote' is <b>bold</b>Code: Select all
A 'quote' is <b>bold</b>Code: Select all
A \'quote\' is <b>bold</b>- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
Oops...I did not know that I should see the output in the view source rather at the browser.
normally, to disable scripts and preserve line breaks and formatting , should I use...
(or)
normally, to disable scripts and preserve line breaks and formatting , should I use...
Code: Select all
$str = nl2br(htmlentities($str))Code: Select all
$str = htmlentities(nl2br($str))