Dealing with HTML characters in PHP

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
codemonkey
Forum Newbie
Posts: 6
Joined: Thu Oct 29, 2009 6:06 pm

Dealing with HTML characters in PHP

Post by codemonkey »

Hello,

I have a php script that takes what a user enters in a form and sends it to a MySQL database. (The form uses an opensource WYSIWYG to write the HTML) Problem is, when my page retreives the data from the MySQL database, it looks like this:

Code: Select all

<p style=\"font-weight: bold;\">
... and the result is that my text doesn't take on whatever properties the user gave it when submitting the form.

How do I prevent it from putting a "\" in front of every quotation?

Thanks,
-Jonathan
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Dealing with HTML characters in PHP

Post by requinix »

Looks like you have magic_quotes on. It's a php.ini setting and with it on PHP will addslashes to form input and URL arguments.

Best thing you can do is turn off magic_quotes. It's a horrible thing anyways - the sooner you get rid of it, the better.
codemonkey
Forum Newbie
Posts: 6
Joined: Thu Oct 29, 2009 6:06 pm

Re: Dealing with HTML characters in PHP

Post by codemonkey »

Thank-you,

For anybody with a similar problem, here's how I solved it:

When storing the data to my variable, I added stripslashes() like this:

Code: Select all

$message = stripslashes(mysql_real_escape_string($_POST['message']));
This got rid of all the "\" and allowed my html code to work properly!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Dealing with HTML characters in PHP

Post by requinix »

Pretty sure you got that backwards.

Code: Select all

$message = mysql_real_escape_string(stripslashes($_POST['message']));
Post Reply