Im getting information from a mysql database and displaying the text like for example:
"A simple - line like this"
it will show as
�A simple � line like this�
its been bothering me and i haven't figured it out
Im getting information from a mysql database and displaying the text like for example:
"A simple - line like this"
it will show as
�A simple � line like this�
its been bothering me and i haven't figured it out
The encoding of your database does not match the encoding of your PHP file. It should be easy enough to add an encoding specification in the HTML, though you may need to do more than this if you plan to display different character sets such as other language than English, especially those with completely different characters (like Chinese or Arabic). For standard symbols like quotes and hyphens, you will be fine using basically any encoding as long as it is consistent throughout the system.
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
Should he go into his phpmyadmin and set the MySQL connection collation to something like utf8_general_ci or something like that? I am pretty sure that you can set this in your php.ini file as well with something like
however I don't understand this as well as djr33. I am mostly curious if my suggestions would help in this situation.Code:AddDefaultCharset UTF-8
Last edited by james438; 02-13-2011 at 07:05 AM. Reason: typo
To choose the lesser of two evils is still to choose evil. My personal site
All that I know for certain is that the encodings don't match at all levels. Setting the encoding of the HTML to match the current encoding of the database will be the easiest way to fix this.
UTF8 might be part of this, and it might not. There are many (dozens? hundreds?) of encodings, though "ISO 8859-1"/"Latin1" (or whatever it's officially called) and UTF8 are certainly very common.
What you are describing may fix the problem, but it must be the right circumstances for that to work. If the problem is that PHP is not using UTF8, then that will work. However, that's not too likely, because the characters being encoded here aren't the sort that PHP has trouble with. Though PHP might not know what characters it is dealing with (and you should fix PHP if possible), the real question is whether they will render correctly in the HTML. PHP grabs data (imagine this as 1s and 0s) from the database, then it prints it to HTML. Whether or not PHP can read it properly, it generally can transfer it without much trouble. If escaping, regex, or other string functions are involved in the PHP, that might cause problems, though.
The one thing that is a bit misleading about your post is that the default character set for PHP and the collation in MySQL (set in PHPMyAdmin if you'd like) are two entirely separate values.
In fact, there are at least 5 places where the encodings can not match up: 1. The database/tables; 2. the PHP file/parser; 3. the HTML (the meta tag at the top); 4. the text-file encoding of the php/html file (the actual file data saved on the server); 5. the server's method of sending the page to the browser. (And 6. the browser might not understand an encoding, but that won't be a problem if you use one that is widely supported. And 7. a certain font or system may not have certain characters, but this isn't a problem except for symbols in rare non-western languages, at least on most devices.)
That said, it's most often a problem with the HTML and the database: set the HTML to match the current encoding of the database, and the problem should be solved.
You can also change the database encoding to match the HTML, but the problem is that current data may (probably will) become corrupted in the process. And one of the hardest things to do in all of this is get incorrectly encoded text re-encoded properly.
So, simple tutorial:
1. Find out the collation of the database.
2. Add the following tag, or a tag like it, to the head section of all your pages:
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
lanceydavid, did this help answer your question? I don't mean to give too much information, and hopefully it will be easy to fix. If you are still having trouble after trying to follow this, it might be a case where you need to do more, such as looking at the intermediate steps involved.
Additionally, if you do plan to display non-western languages (such as Chinese or Arabic), then you will need to use UTF-8 (or a language-specific encoding, like BIG5 for Chinese), and the process may become more complex in a few ways.
James, did that clarify a bit? Your information isn't wrong, but it may not apply in all cases. I recommend setting everything to UTF-8 always, but that's because I tend to make websites that contain at least some text in various languages, and unicode is the only way to guarantee that will go smoothly. So set every step to UTF-8 and you won't have a problem. But that's not the easiest way to patch things up, or necessarily what everyone wants, such as if they want to use the default (non unicode) option, and that actually is said to be faster by some people, though unicode isn't slower on a scale that humans would notice, just at a very technical level. But to each his own...
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
james438 (02-13-2011)
Thanks for clarifying. I wanted to ask you more about this, but I was not sure how to ask it. That was a very useful/handy post by the way.
To choose the lesser of two evils is still to choose evil. My personal site
Sure. Let me know if you have any more (specific) questions about encodings, though what I've found out is just from personal trial and error experience.
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
Bookmarks