Mike Rifgin
Mike Rifgin

Reputation: 10757

encode html in php and decode in javascript

I have some html code I want to store in a database. I need a way to encode it in php so all the special characters don't break the db INSERT (the html can include all sorts of spec chars) and then a way to unencode that at the other end in javascript once i've passed it via JSON so that the html is rendered correctly.

IS there any way I can do this?

Upvotes: 0

Views: 1611

Answers (2)

user7675
user7675

Reputation:

Regarding "not breaking the db INSERT," this should be a completely moot point. You should either be appropriately escaping all user-provided data (eg. mysqli_real_escape_string) or using binding.

Upvotes: 1

Quentin
Quentin

Reputation: 943630

Since you are using PHP:

For the database, use PDO: http://bobby-tables.com/php.html

And for the JSON, use the json methods: http://php.net/json

These handle all the escaping for you.

Upvotes: 1

Related Questions