Htmlentities or html_entity_decode

I have a page with a form where users write a code in swedish with åäö and ÅÄÖ. It’s UTF-8 on the page and in the db as well. But I need to store the text as it is.
When it’s stored in the table it looks like this: ÅÄÖ åäö
I just want it to be åäö ÅÄÖ and not like the above and not like this & auml ;

What do I need to convert it to the correct chars?
I think I have tried almost everything I can come up with, but with no luck.
The MySQL table is also set to UTF-8

How are you viewing the tables values, from the command line? eg.

mysql> SELECT `countries`.`name` AS `country_name`,
    ->  `states`.`name` AS `state_name`,
    ->  `cities`.`name` AS `city_name`
    ->  FROM `countries`
    ->  INNER JOIN `states` ON `countries`.`id` = `states`.`country_id`
    ->  INNER JOIN `cities` ON `states`.`name` = `cities`.`state_name`
    ->  WHERE `countries`.`id` = 5 ORDER BY `cities`.`name`;
+--------------+-------------------+-------------+
| country_name | state_name        | city_name   |
+--------------+-------------------+-------------+
| Sweden       | Västra Götaland   | Gothenburg  |
| Sweden       | Skåne             | Helsingborg |
| Sweden       | Stockholm         | Huddinge    |
| Sweden       | Jönköping         | Jönköping   |
| Sweden       | Östergötland      | Linköping   |
| Sweden       | Skåne             | Malmo       |
| Sweden       | Örebro            | Örebro      |
| Sweden       | Stockholm         | Stockholm   |
| Sweden       | Uppsala           | Uppsala     |
| Sweden       | Västmanland       | Västerås    |
+--------------+-------------------+-------------+
10 rows in set (0.07 sec)

I am looking at it in phpmyadmin. The fields look like that.

The output is then used in a mobile app which is already ready and coded.
When I write the text in the db fields in phpmyadmin as it’s supposed to looks correct in the app.
But when I insert it from my page it puts the wrong chars in the table.
That’s why I need to insert it as it is.

When I use this:

$_POST['object_descr'] = htmlentities($_POST['object_descr'], ENT_QUOTES);

I get something like & auml ; stored in the db.
But I want the actual char stored. Like it looks in print.

There are HTML entities and there are HTML “special characters”.

For example < can be &lt; or &#60;

For example < can be < or <

Simlarly, ä can be &auml; or &#228;

Simlarly, ä can be ä or ä

The < is a “special character” that has an entity, the ä has an entity but is not a special character.

http://php.net/manual/en/function.htmlentities.php

This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.

So maybe you want htmlspecialchars() instead?

Nope. That didn’t work either…

Try to make sure everything is utf-8. Your scripts charset, the file save format, the table collation and, the bit that caught me out recently, the connection to the database.
I had everything but the connection set utf-8, and when I pulled a pound sign from a table and displayed it, it showed as a “mystery symbol”. Once I set the connection to utf-8, everything was fine.

1 Like

actually, you don’t want any of the escaping function as their purpose is to escape data for HTML output (and when using UTF-8, only htmlspecialchars() is necessary).

have you set the DB connection to UTF-8? (that is NOT the same as defining the character set in the DB!)

1 Like

I will make a new blank document and try this…

Thanks a lot. That one solved it. This forum is great.
I just added this to the connection:

mysql_set_charset("utf8", $con);

also note that mysql_* functions are not safe to use (removed in PHP 7 and no support for Prepared Statements). better use PDO or MySQLi!

Ok. I did that on all my pages except the connection which is something I took from an old installation I had…

no. parameters have changed as well. refer to the respective manual entries for the correct way to use it.

additionall, mysqli allows you to use prepared statements, which are immune to sql injection attacks.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.