oliverbj
oliverbj

Reputation: 6052

php/mysql - foreign characters

I am having problems displaying foreign characters (characters with accents like: é à ù ç and so on)

The row in the database is like this:

    Name | Datatype | Charset 
title | varchar(255) | utf8_general_ci

I store it like this:

function inputFilter($var)
{
    $var = trim(htmlentities(strip_tags($var)));

    if (get_magic_quotes_gpc())
        $var = stripslashes($var);

    $var = mysql_real_escape_string($var);

    return $var;
}
$title = inputFilter($_POST['title']);

and I print it like this:

print $getfromdb['title'];

This is how it's printed out:

Português //Should be: Português

I have tried adding: htmlspecialchars, utf8_decode/encode and htmlentities to the print, although nothing helps!

I've added this to the header:

<meta charset="utf-8">

What am I doing wrong?

Upvotes: 4

Views: 4480

Answers (5)

Rajesh Paul
Rajesh Paul

Reputation: 7019

You should use character encoding as ISO-8859-1 instead of UTF-8 as follows:

<meta charset="ISO-8859-1">

The characters you are trying to show are latin and UTF-8 i.e. UNICODE encoding cannot interpret latin characters.

Reference

And in case of mysql you should use latin1 charset.

Upvotes: 0

Minoru
Minoru

Reputation: 1730

Include mysqli_set_charset($link, "utf8"); right after every connection you make. This will work.

Upvotes: 2

Farid Movsumov
Farid Movsumov

Reputation: 12725

Use SET NAMES utf8 before you query/insert into the database

query("SET NAMES utf8");

Upvotes: 1

Jenson M John
Jenson M John

Reputation: 5689

Steps to Follow:

Use the meta tag for UTF8.

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

Set your PHP to use UTF8.

mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
mb_http_input('UTF-8');

For mysql, you want to convert your table to UTF8.

ALTER TABLE table_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci

Also you can run:

 SET NAMES UTF8

as the first query after establishing a connection which will convert your DB connection to UTF8.

Upvotes: 2

Walter Tross
Walter Tross

Reputation: 12624

Try this:

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

<meta charset="UTF-8"> is HTML 5 (and, BTW, UTF-8 is uppercase)

Since it looks like your test chain also involves a form (because of the $_POST), you must make sure that the UTF-8 charset is set for the form too.

Upvotes: 1

Related Questions