10

I am getting the lovely � box where spanish characters should be displayed. (ie: ñ, á, etc). I have already made sure that my meta http-equiv is set to utf-8:

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

I have also made sure that the page header is set for utf-8 also:

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

Here is the beginning stages of my code thus far:

<?php
    setlocale(LC_ALL, 'es_MX');
    $datetime = strtotime($event['datetime']);
    $date = date("M j, Y", $datetime);
    $day = strftime("%A", $datetime);
    $time = date("g:i", $datetime);
?>
    <a href="/<?= $event['request'] ?>.html"><?= $day ?> <?= $time ?></a> 

The above code is in a where statement. I have read that switching the collation in the database can also be a factor but I already have it set to UTF-8 General ci. Plus, the only thing that is in that column is DateTime anyway which is numbers and cannot be collated anyway.

result: s�bado 8:00

Any help is greatly appreciated as always.

NotJay
  • 3,583
  • 5
  • 34
  • 61
  • did you force the php->mysql database connection to be UTF as well? having a db and your php output in UTF is pointless if the php->mysql connection is something else. the **ENTIRE** rendering pipeline has to be the same charset, or joined with appropriate charset conversion logic... if there's a mismatch at any stage, you'll get corrupted chars. – Marc B Feb 01 '13 at 15:31
  • Is default_charset for php set to utf-8 ? – PoX Feb 01 '13 at 15:33
  • Yes, I set the connection to UTF 8 also... mysql_set_charset('utf8', $connnection); – NotJay Feb 01 '13 at 15:34
  • The default_carset... would that be in the php ini file? – NotJay Feb 01 '13 at 15:35
  • yes php ini has default_charset, if you do not have access to it use ini_set('default_charset', 'UTF-8'); – PoX Feb 01 '13 at 15:39
  • okay, I have set it there as well and it seems to do nothing. default_charset = "utf-8" – NotJay Feb 01 '13 at 15:41
  • So I have a temporary switch set in place to translate the days of the week. After doing this, the special characters work. However, I still need to figure out what is going wrong as this is only a small bandaid. – NotJay Feb 01 '13 at 16:14
  • I have a MySql database with a table that has a column (DateTime) and from there, I am trying to take the DateTime format, convert it for PHP and then translate it using setlocale(LC_ALL, "es_MX") and strftime. There are a lot of pieces here which makes it harder to investigate. Additionally, the DB connect file, page layout, header and body content are all separate files controlled by the htaccess. I will keep this question open for now and keep you posted if I find the issue. If anyone has suggestions, I would also be open to them. Thanks again! – NotJay Feb 01 '13 at 16:17

7 Answers7

11

Things to consider in PHP/MySQL/UTF-8

  • The database tables and text columns should be set to UTF-8
  • HTML page Content-Type should be set to UTF-8

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

  • PHP should send a header informing the browser to expect UTF-8

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

  • The PHP-MySQL connection should be set to UTF-8

    mysqli_query("SET CHARACTER_SET_CLIENT='utf8'",$conn);

    mysqli_query("SET CHARACTER_SET_RESULTS='utf8'",$conn);

    mysqli_query("SET CHARACTER_SET_CONNECTION='utf8'",$conn);

  • PHP ini has default_charset setting it should be utf-8 if you do not have access to it use ini_set('default_charset', 'utf-8');

PoX
  • 1,192
  • 18
  • 32
  • Warning: mysqli_query() expects parameter 1 to be mysqli, string given – NotJay Feb 01 '13 at 16:02
  • $conn = mysql_connect("host", "user", "pass"); – NotJay Feb 01 '13 at 16:19
  • here's the rest... if(!$conn) { die(); } else { mysql_select_db("dbname"); } – NotJay Feb 01 '13 at 16:23
  • you are using mysql_connect then try mysql_query("SET CHARACTER_SET_CONNECTION=utf8"); This might help http://www.ustrem.org/en/articles/utf8-website-building-en/ – PoX Feb 01 '13 at 16:48
  • 1
    Please use `utf8mb4` instead of `utf8`. More information here: https://medium.com/@adamhooper/in-mysql-never-use-utf8-use-utf8mb4-11761243e434 – ManInTheBox Jan 29 '18 at 15:38
6

I have suffered this problem for many years and I can't find any logic and I have tried all the solutions above.

One solution is to make html codes for all text. Here is a function I have used when all else has failed.

function span_accent($wordz)
{

$wordz = str_replace( "Á","&Aacute;",$wordz);
$wordz = str_replace( "É","&Eacute;",$wordz);
$wordz = str_replace( "Í","&Iacute;",$wordz);
$wordz = str_replace( "Ó","&Oacute;",$wordz);
$wordz = str_replace( "Ú","&Uacute;",$wordz);
$wordz = str_replace( "Ñ","&Ntilde;",$wordz);
$wordz = str_replace( "Ü","&Uuml;",$wordz);

$wordz = str_replace( "á","&aacute;",$wordz);
$wordz = str_replace( "é","&eacute;",$wordz);
$wordz = str_replace( "í","&iacute;",$wordz);
$wordz = str_replace( "ó","&oacute;",$wordz);
$wordz = str_replace( "ú","&uacute;",$wordz);
$wordz = str_replace( "ñ","&ntilde;",$wordz);
$wordz = str_replace( "ü","&uuml;",$wordz);

$wordz = str_replace( "¿","&iquest;",$wordz);
$wordz = str_replace( "¡","&iexcl;",$wordz);
$wordz = str_replace( "€","&euro;",$wordz);
$wordz = str_replace( "«","&laquo;",$wordz);
$wordz = str_replace( "»","&raquo;",$wordz);
$wordz = str_replace( "‹","&lsaquo;",$wordz);
$wordz = str_replace( "›","&rsaquo;",$wordz);
return $wordz;
}
4

Having a similar problem, I found the answer here. Not Displaying Spanish Characters

The resolution was to change from UTF-8 to windows-1252.

(HTML) <meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
(PHP) ini_set('default_charset', 'windows-1252');

My problem was reading Spanish characters from a CSV file. When I opened the file in Excel, the characters appeared fine. In my editor, the odd character was shown regardless of the intended character. This change seems to work for my requirements.

DanimalReks
  • 131
  • 2
  • 10
3

Kindly check your file ENCODING. It must be in UTF-8 or UTF-8 without BOM.

To change you file encoding. Use Notepad++(you can use also other editor where you can change the file encoding). In menu bar > Choose ENCODING > Choose any UTF-8 or UTF-8 without BOM.

See link for the difference of UTF-8 and UTF-8 without BOM. What's different between UTF-8 and UTF-8 without BOM?

Hope it can help. :)

Community
  • 1
  • 1
Dawn Menor
  • 51
  • 2
0

it's important to check that your code is also codified as UTF-8 (you can see this property in a lot of text and code editors).

Because there is only one symbol (the black square), its probably that you are using ISO-8859-1 or ISO-8859-15 .

castarco
  • 1,334
  • 2
  • 16
  • 32
0

Can you see that the content is correct in the database table, look at it with phpmyadmin for eg. If it is, be sure your php files are utf8 encoded, take a look at your ide/editor configuration.

tkorkunckaya
  • 164
  • 3
  • 15
0

Use utf8mb4 or Windows-1252

ini_set('default_charset', 'utf8mb4');

or

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

then use tag,

<meta charset="utf8mb4">
Optimaz ID
  • 391
  • 3
  • 9