Abs
Abs

Reputation: 57916

Safely Store HTML in DB without affecting Character encoding

I want to take a string and store it in a MYSQL db. This string will be a HTML string and it can have any character encoding or be written in any language.

How can I safely save this in my MYSQL DB without affecting the HTML string so that I can later retrieve it as it is?

In addition, the field it will be stored in is of data type text and has a collation of latin1_swedish_ci will this effect it in anyway?

I am currently doing this:

htmlentities($html, ENT_QUOTES, 'UTF-8')

But I don't think the above will work for all character sets. I mean how will German or Japanese characters be affected?

Thanks for any help.

Upvotes: 0

Views: 1076

Answers (4)

Lex
Lex

Reputation: 5014

Interesting everyone is suggesting base64, I never thought about doing it that way. I know a lot of CMS databases I've used just use utf-8 character encoding. This will support your germany and japanese characters. The HTML shouldn't get affected, and will render in the browser fine as long as the HTML charset is also utf-8 charset=utf-8

Upvotes: 0

PPC-Coder
PPC-Coder

Reputation: 3632

I don't think the collation won't have an effect on the storage of values. It would only affect the behaviour for when you do things like comparisons (WHERE) and sorting (ORDER BY).

IMHO, the safest way to ensure your data is unaltered would be to store the values as Binary. Base64 would also work. In either case, you would have to know the character encoding when reading it back out though.

Upvotes: 0

fab
fab

Reputation: 1859

You could store it in a BLOB field and MySQL will never ever try to convert it. But that means that you have remember the the encoding you used when saving the string.

Another option is to encode the string as base64.

Upvotes: 0

mway
mway

Reputation: 4392

Why not base64 encode it for storage, and then decode it after?

Upvotes: 2

Related Questions