28

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?

robert
  • 811
  • 3
  • 16
  • 28

9 Answers9

51

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.

RCE
  • 1,681
  • 13
  • 12
  • 1
    I have the meta tag that specifies charset=utf-8 in my header. Adding AddDeafultCHarset UTF-8 to my .htaccess doesn't help. I'd rather avoid solution directly from PHP code as it's a pretty complex site. – robert Dec 02 '11 at 12:44
  • 5
    My problem has usually been that the actual files were not in UTF-8 format. For example the character `ä` of ASCII will show as `�` if the browser reads it as UTF-8 but the actual file isn't. – RCE Dec 02 '11 at 13:00
  • The actual files are definitely utf-8, and they worked perfectly on my EasyPHP. – robert Dec 02 '11 at 13:04
  • 1
    GREAT!!! This worked for me (I'm using WordPress.... stupid WordPress!!!): AddDefaultCharset UTF-8 Thanks a lot! – Santiago Baigorria Mar 10 '13 at 19:51
  • I had php set to utf-8 everywhere, like you all said. But, as @RCE said, the files were not in UTF-8. I converted all of them using Notepad++, saving them as UTF-8. The accented chars where then converted to squares and, when running the site, the squares became the question mark inside the diamond.. I had to manually review each accent in each static page in order to assure the portuguese characteres (ç, á, ã, é, à, etc) are ok after the change. – Mário Meyrelles May 20 '13 at 21:28
6

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • Thanks, but I don't have php.ini available. The browser says the encoding is utf-8, and it displays the same in all browsers. I have the proper encoding declared in my header. I've tried adding it to .htaccess anyway but that did nothing. Do you think it could be a problem with the hosting account somehow? – robert Dec 02 '11 at 12:50
  • Using ini_set displays the characters but it doesn't read any PHP content then! – robert Dec 02 '11 at 13:01
  • If the browser reports UTF-8 and it does not properly display, your data is broken somewhere. Encoding is like a chain, if one element is wrong, the end result is wrong. Take care and locate the element of the chain that is not working. And what do you mean by "any PHP content then"? – hakre Dec 02 '11 at 13:03
  • I have a PHP header included (just a banner with some links). When I use the ini_set function, the page doesn't show the header - it acts like I'd renamed it to .html. The files displayed properly on my EasyPHP, how could they have gotten broken by being uploaded to a hosting account? – robert Dec 02 '11 at 13:06
4

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!

user2842936
  • 67
  • 1
  • 3
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');
VVV
  • 7,563
  • 3
  • 34
  • 55
3

I solve my issue by using utf8_encode();

$str = "kamé";

echo utf8_encode($str);

Hope this help someone.

Kamran Sohail
  • 602
  • 7
  • 13
0

If all the other answers didn't work for you, try disabling HTTP input encoding translation.

This is a setting related to PHP extension mbstring. This was the problem in my case. This setting was enabled by default in my server.

Binod Kalathil
  • 1,939
  • 1
  • 30
  • 43
0

The problem is because your file are not with the same encoding. First run the following command in all your files:

file -i filename.* 

In order to fix the problem you have to change all your files to uft-8. You can do it with the command iconv:

iconv -f fromcode -t tocode filename > newfilename

Example:

iconv -f iso-8859-1 -t utf-8 index.html > fixed/index.html

After this you can run file -i fixedx/index.html and you will see that your file is now in uft-8

Vega
  • 27,856
  • 27
  • 95
  • 103
Mauricio
  • 9
  • 1
0

Adding the following line in the head tag fixed my issue.

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
Hiren Parghi
  • 1,795
  • 1
  • 21
  • 30
0

If none of the above work, it could be the mysqli connection encoding.

In my case, the php file (whatever.php) was UTF-8, the html was declared as UTF-8, and the database was even in UTF-8, yet the characters were still not showing up correctly.

Apparently, mysqli decided to default to a different encoding.

So I added

$conn->set_charset("utf8");

after

$conn = new mysqli($servername, $username, $password, $dbname);

and it worked.

If your database tables are utf8mb4_unicode_ci and not utf8_general_ci, then put this instead:

$conn->set_charset("utf8mb4");

Go into phpmyadmin (or your preferred database manager) and check which encoding your tables are in to select the correct encoding.

Ref: https://www.php.net/manual/en/mysqli.set-charset.php

Scott M. Stolz
  • 744
  • 1
  • 5
  • 11