help with text formatting from textarea

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vchris
Forum Contributor
Posts: 204
Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec

help with text formatting from textarea

Post by vchris »

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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

monospace-font? nl2br()?
vchris
Forum Contributor
Posts: 204
Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec

Post by vchris »

Thanks a lot man it works! That was fast!!!

:lol:
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

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???

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>";
}

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you run nl2br on the output of htmlentities. ;)
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

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>";
}

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

is there still an issue or are you just showing the results?
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

sorry I forgot to say that htmlentities is not working when you see the result... :oops:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your second echo in the submission processing block looks exactly as it should for just running htmlentities().
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

the example from PHP manual

input

Code: Select all

A 'quote' is <b>bold</b>
should output

Code: Select all

A 'quote' is <b>bold</b>
but mine outputs as

Code: Select all

A \'quote\' is <b>bold</b>
ofcourse, i see a difference :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you're viewing it on a webpage. Look at the source code. It has exact what the manual says will output. :P
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

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...

Code: Select all

$str = nl2br(htmlentities($str))
(or)

Code: Select all

$str = htmlentities(nl2br($str))
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the former.
Post Reply