utf-8 special characters not displaying

136,794

Solution 1

This is really annoying problem to fix but you can try these.

First of all, make sure the file is actually saved in UTF-8 format.

Then check that you have <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> in your HTML header.

You can also try calling header('Content-Type: text/html; charset=utf-8'); at the beginning of your PHP script or adding AddDefaultCharset UTF-8 to your .htaccess file.

Solution 2

It sounds like that if you request faq.html the webserver signals your browser that the file is in UTF-8 encoding.

Check that with your browser which encoding is announced and used, please see the documentation of your browser how to do that. Every browser has this, most often accessible via the menu (to specify your preference which website's encoding should be used) and to see what the server returned, you often find this in page properties.

Then it sounds like that if you request faq.php the webserver singals your browser that the file is in some other encoding. Probably no charset/encoding is given as per default PHP configuration setting. As it's a PHP file you can most often solve this by changing the PHP configuration default_charsetDocs directive:

default_charset = "UTF-8"

Locate your php.ini on the host and edit it accordingly.

If you don't have the php.ini available, you can change this by code as well by using the ini_setDocs function:

ini_set('default_charset', 'UTF-8');

Take care that you change this very early in your script because PHP needs to be able to send out headers to have this working, and headers can't be set any longer if they have already been send.

Manually sending the Content-Type header-line does work, too:

header('Content-Type: text/html; charset=UTF-8');

Additionally it's good practice that all the HTML pages you output have this header as well in their HTML <head> section:

<html>
  <head>
    ...
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    ...

Hope this is helpful.

Solution 3

set meta tag in head as

 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 

use the link http://www.i18nqa.com/debug/utf8-debug.html to replace the symbols character you want.

then use str_replace like

    $find = array('“', '’', '…', '—', '–', '‘', 'é', 'Â', '•', 'Ëœ', 'â€'); // en dash
                        $replace = array('“', '’', '…', '—', '–', '‘', 'é', '', '•', '˜', '”');
$content = str_replace($find, $replace, $content);

Its the method i use and help alot. Thanks!

Solution 4

If you're using PHP and none of the above worked (as it was my case), you need to set the locale with utf-8 encoding.

Like this

setlocale(LC_ALL, 'fr_CA.utf-8');

Solution 5

I solve my issue by using utf8_encode();

$str = "kamé";

echo utf8_encode($str);

Hope this help someone.

Share:
136,794
robert
Author by

robert

Updated on July 09, 2022

Comments

  • robert
    robert almost 2 years

    I moved my website from my local test server to NameCheap shared hosting and now I'm running into a problem - some of the pages aren't displaying utf-8 special characters properly (showing question marks instead). All pages are utf-8 encoded, as are all database tables. The strange thing is, some pages display correctly and some don't, in a seemingly random pattern.

    For instance, my index page is fine, but my profile page isn't. faq.html works fine, but when I rename it to faq.php it doesn't. And weirdest of all, I have a page with two JQuery tabs where one displays correctly and the other doesn't!

    Can someone help me out with this?