Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

php - There are symbols like  and so on in database, what to do?

I have a few symbols in my description like ? a € and so on. Can I do anything about it? Or if it's in database, I can't do nothing now?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It sort of depends what the problem actually is...

If it's that those characters are supposed to be there (such as "Ma?ana" in Spanish) then you'll need to ensure everything is in UTF-8... the best way is to:

1: check the database tables are in "utf-8" encoding (if not convert them to utf-8)

2: as Martin noted, ensure the database connector is utf-8 using something like:

mysql_set_charset('utf8'); //note that MySQL uses no hyphen here

3: ensure the the document is utf-8 (you can add a header at the top)

<?php header('Content-type:text/html;charset=utf-8'); ?>

4: just to be on the safe side, add it in as a meta tag as well

<meta http-equiv="content-type" content="text/html;charset=utf-8" />

HOWEVER

It's quite possible you've got some duff characters in the database where something like ISO-8859-1 has been juggled to UTF-8, badly. In this case you'll notice things like ?£ where what you actually want is (because UTF-8 characters contain more data than ISO-8859-1 characters, that extra data can become an additional character if you're not careful).

In which case your best bet is to clean the database (you could probably do something like UPDATE table SET field = REPLACE(field, '?£', '£') for common "errors") and then convert the whole kaboodle to UTF-8 (as outlined above) to avoid the problem recurring.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...