Show line breaks instead of <br /> in text field.

Hello,

I am having some trouble with a user text field inside a form. I want the textfield to show new lines instead of
lines. When I echo or print the entered data out it shows up correctly without the
tags and new lines are created with out any trouble.

However when I print back what the user has typed into that field so they can make any edits to is in the future it shows the
instead of line breaks and if they submit the data again it just appends a new
again.

What I currently has as my script
[php]$var = $_POST[‘user_input’];
$var = nl2br(htmlspecialchars($var));
$var = mysql_real_escape_string($var);[/php]

When I echo it on the users page it shows up correctly. So if a user inputs:

"Hi,

Have A Nice Day"

That’s what the member can see on their profile page. But inside the text field on the edit page they see the following:

Hi,



Have A Nice Day

if they hit the submit button again they see:

Hi,





Have a nice day

so on and so forth.

I want them to only see line breaks in the text field instead of the
tags and I really don’t want to add more
tags at every line-break when resubmitted . When I echo or print it out I am simply placing echo/print $var. What am I doing wrong as I did have it working but something happened and I lost the file and I’m trying to do everything I can to figure it out again.

Thanks for any help.

Anthony

Well, you are using the function “nl2br()” which changes the NL’s to BR’s.
So, to remove the BR’s, there is no br2nl function.

You have to use replace… Something like this:

$var = str_replace(’
’, “\n”, $var);

Hope that helps…

Hello, thank you for the reply.

I fixed the problem by removing the nl2br() out of the submitted data and using it just when I print($var) on the page out of the database. This got rid of the
tags from showing but at the same time keeping the line breaks.

Of course I put $var into a variable on the page by creating an array out of the information collected by the data base and then I use the str_replace() to replace all instances of \ to hide the escapes created by the mysql_real_escape_string.

There is a lot of work to protect your database and I’m sure I’m just seeing the tip of the iceberg of research I have to do prior to making my script go into a live uncontrolled test environment.

Thank you for your help again :slight_smile:

Glad you solved it… Just FYI:

I use the str_replace() to replace all instances of \

There is a PHP function to do this for you. “stripslashes” and one to add them in “addslashes”…

http://php.net/manual/en/function.stripslashes.php

CYA in the bitstream…

Sponsor our Newsletter | Privacy Policy | Terms of Service