Click to See Complete Forum and Search --> : trimming text within textareas
JanKok
04-09-2004, 03:29 PM
Hi, I have a question which is may stupid?
To trim variables I am using such as;
$msg=trim($_POST['msg']);
this seems however not a good idea for a text within a textarea because I lost all formattings of course.
How can I set the variable $msg without trimming the text and keep the formatting as it was entered?
Jan
Conor
04-09-2004, 04:05 PM
what do you mean you just want the stuff from the text area in a variable?
like
$msg=$_POST['textarea'];
or
$msg=$_GET['textarea'];
depends on if your using post or get as form method and im not sure if this is what you meant anyway
JanKok
04-09-2004, 04:27 PM
Yes, just the stuff in avariable.
Now if I look at your reply I think that I found what I am doing wrong here.
I tried this before:
$msg=($_POST['textarea']);
I will do it your way now witout putting it between the ()
Thanks
JanKok
04-09-2004, 08:27 PM
This have the same effect,
What I meant is that I like to save the formatting of the text which was entered by the user.
e.g. >>>
Hello guys,
blabla
Thanks
<<<
If the above text is submitted then it looks like this:
Hello guys, blabla Thanks
Is this not possible?
Conor
04-09-2004, 08:43 PM
o so you want the spaces put in to stay as spaces
$textarea=$_POST['textarea'];
$msg=nl2br($textarea);
that should do it
Paul Jr
04-09-2004, 08:44 PM
I don't have PHP up and running yet, so I can't test this, but you might wanna try specifying with the optional second parameter to strip only spaces:
$msg = trim($_POST["textarea"], " ")
trim() (http://www.php.net/trim)
The Cheat
04-09-2004, 11:17 PM
ok,
trim should have nothing to do with losing your formatting. trim() will only remove extra space before and after a string, nothing inbetween, So your problem is not related to trim at all.
My guess is that you are storing information in some sort of database then pulling it out to be displayed on a page. As refreshf5 said, What you should do is run it thought the nl2br() function while it is being retrieved from a database. nl2br = New Line to <br>. It converts new lines in a string to <br> so it is formatted properly in html.
And if you're trying to keep spaces, like if I put three spaces in a row, and you want it to output three spaces in a row, convert the spaces into &nbsp;'s so that they will display the actual spaces in the HTML as well as the textarea. For example:
$msg = str_replace(" ","&nbsp;",$msg);
To clarify, since I don't think I explained very well, this will remove spaces and put &nbsp; in its place, which will make the spaces appear in the HTML (PHP) document.
JanKok
04-10-2004, 08:31 AM
Thank You ALL so much for your help.
The suggestion from RefreshF5 works PERFECT but now I have to go to php.net I gues to learn more about the n12br because I do not understand what I am doing here.
Again, thanks
Jan
Conor
04-10-2004, 08:49 AM
no problem basically all it does is convert all /n's to <br> tags so it will work correctly in html stuff
JanKok
04-10-2004, 09:21 AM
Perfect explained, Thanks
The Cheat
04-10-2004, 03:32 PM
lol if you read my post above....
Originally posted by The Cheat
ok,
trim should have nothing to do with losing your formatting. trim() will only remove extra space before and after a string, nothing inbetween, So your problem is not related to trim at all.
My guess is that you are storing information in some sort of database then pulling it out to be displayed on a page. As refreshf5 said, What you should do is run it thought the nl2br() function while it is being retrieved from a database. nl2br = New Line to <br>. It converts new lines in a string to <br> so it is formatted properly in html.