utf8_encode and accented characters dilemma? - php

I am facing a paradox in decoding with utf8_encode decode. I have a MySQL database with uft8 collation and whose fields have the utf8_general coding. I have my php file in utf8, and in my HTML pages I have specified in the header the utf8 charset.
My problem is that when I select from my table a field containing accented characters (like èçò ùé) and echo that to the browser, I get strange characters.
To resolve my problem, I have to echo $description=utf8_encode($imm['description']).
My question is why can’t I do the echo directly without having to use uft8_encode every time?

I'll just guess that your database connection is not set to UTF-8.
See SET NAMES utf8 in MySQL?

you need to specify the header using php to be utf-8. also make sure that the format of the chars is utf-8 before storing in the db because utf_encode encodes an ISO-8859-1 string to UTF-8, which most likely means that the chars are being stored as ISO-8859-1 in s a utf-8 table.
make sure that you convert those chars in utf-8 before storing them in the db and then echo should not be a problem at all.
Source: had the exact same problem myself.

Related

How to echo database content safely without foreign characters

I built a PHP web page and some of the characters are like this
⇾ ....
â€....
etc
Please help me on how to eliminate this
If you're not using UTF-8 characterset, I would try to change the encoding of the database to avoid these characters:
How to convert an entire MySQL database characterset and collation to UTF-8?

Which function encodes accentuated letters in PHP?

There is a MySQL database and I want to select columns from a table.
I must return a String from the concatenation of the selected column values. But in one of the columns there are accentuated letters in the column value , like é.
So how to encode the column returned value ?
NB : I already wrote header('Content-Type: text/plain; charset=utf-8'); at the beginning of the PHP file.
Defining charset with SET NAMES 'utf-8' may help.
http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html
What encoding is your database table in? On a lot of installations, MySQL defaults to LATIN-1. Make sure the table stores its data as UTF-8, then make sure that the connection between MySQL and PHP is in UTF-8. The easy way to do that is running the query SET NAMES utf8 after connecting, but you can also set a default encoding.
Next, the UTF-8 header should be sent from the server to the browser, but you've already done that by adding the header() call.
If your database table is currently not encoded as UTF-8, you might need to re-enter your data after changing it.
Maybe the Multibyte String Module can be of some help to you, part of the Human Language and Character Encoding Support.

why do i have to use mb_convert_encoding($name,'ISO-8859-15','utf-8') to get accented chars to display?

the data im working with here is off of a page that uses utf8 encoding
i've set my database and fields to use utf8_general_ci
now for whatever reason, i have to use the following code on the variable in order to have it display accented characters correctly in the database:
mb_convert_encoding($name,'ISO-8859-15','utf-8');
this makes no sense to me. why do i have to convert it to ISO-8859-15 when phpmyadmin is in utf8, the data is in utf8, and the database and table fields are in utf8?
You most likely have not set your database connection to UTF-8, so your database expects you to send ISO-8859 encoded data. See http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html

MySQL Collation or PHP side to display accented letters properly

What is the best Collation for the column that can allow to store accented letters and parse them out perfectly without any encoding error, because whenever I add an accented letter such as é, å, it shows out with an encoding problem on the PHP side, but in the MySQL side it's fine...
How do I get the accented letters display properly?
You get them correctly by matching the encoding on both ends, ie. both your PHP output and your DB should use the same encoding. For European languages I would suggest using UTF-8 for both your scripts and the DB. Just remember that you still have to initialize UTF-8 collation in MySQL using SET NAMES 'utf8' COLLATE 'utf8_general_ci' (so run this query just after you make a connection to the DB and you should be ok).
Perhaps your problem isn't within the database, but within however you're displaying things from PHP? What content encoding are you specifying in your output? You might need to manually send a header to specify that the content is UTF-8 if that's what you're trying to output.
For instance: header("Content-Type: text/html; charset=UTF-8");

Accented characters stored in MySQL database

I have a webapp that stores French text -- which potentially includes accented characters -- in a MySQL database. When data is retrieved directly through PHP, accented characters become gibbirish. For instance: qui r�fl�te la liste.
Hence, I use htmlentities() (or htmlspecialchars() ) to convert the string to html entities, and all is fine. However, when I come to output data that contains both accented characters and HTML elements, things get more complicated. For instance, <strong> is converted to <strong> and therefore not understood by the browser.
How can I simultaneously get accented characters displayed correctly and my HTML parsed correctly?
Thank you!
Maybe you could take a look to utf8_encode() and utf8_decode()
You should use UTF-8 encoding for storing the data in the database - then everything should work as expected and no htmlentities() will be required.
Make sure all aspect are utf-8 - the database, the tables encoding and collation, and the connection, both on the client and server side. Things might work even if not everything is utf-8, but might fail horribly when you will do backup & restore - that is why I recommend utf-8 across the board.
You could set the Collation of the database fields containing the accented character to utf8_general_ci to support them.
Eventually you can set the collation of the database as well, so all fields are set by default.

Categories