1

I have a database where I need to display the records for the user. I am using htmlentities to make sure no malicious code is being echoed to the user like this:

    function h($string) {
      return htmlentities($string, ENT_SUBSTITUTE, "UTF-8");
    }

then calling the function whenever I output any entries to the user. The problem is that I need to be able to show the Danish characters ÆØÅ and these characters displays as a question mark in a square. The site has utf-8 encoding as well.

I have tried all that is listed under htmlentities on php.net and tried finding some solution for creating exceptions or another work around, but I have been unable to find any.

Does anybody know a workaround for this issue?

MaDaHoPe
  • 133
  • 10
  • I ran your code exactly how it is in your question and it worked for me. – Zac Brown Aug 03 '16 at 15:17
  • 1
    Try doing an initial query `SET NAMES utf8`. If you're using mysqli, you can do: `mysqli_set_charset($link, "utf8");` or if you're using PDO, you can set the encoding when establishing the connection `"mysql:host=$host;dbname=$db;charset=utf8"`. See this question: http://stackoverflow.com/questions/4361459/php-pdo-charset-set-names – Ultimater Aug 03 '16 at 15:17
  • 1
    I'd be amazed if the answer isn't covered by this : http://stackoverflow.com/questions/279170/how-to-support-utf-8-completely-in-a-web-application/279279 – CD001 Aug 03 '16 at 15:18
  • @Ultimater THANK YOU!! So easy and yet so hard to find! Indeed defining the charset in the connection solved it! – MaDaHoPe Aug 03 '16 at 16:05

1 Answers1

0

The second comment answered it. Adding the charset in the connection solved the problem. So for my PDO connection I had to put it like this:

    $dbh = new PDO('mysql:dbname=myName;charset=utf8;host=myHost', 'myUser', 'myPassw0rd');

Now everything displays properly.

SilverlightFox
  • 32,436
  • 11
  • 76
  • 145
MaDaHoPe
  • 133
  • 10