php/mysql-外来字符 [英] php/mysql - foreign characters

查看:77
本文介绍了php/mysql-外来字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在显示外国字符时遇到问题(带有重音符号的字符,例如éàç等)

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

我这样存储它:

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']);

我这样打印:

print $getfromdb['title'];

这是它的打印方式:

Português //Should be: Português

我尝试添加: html特殊字符,utf8_decode/encode和htmlentities进行打印,尽管没有任何帮助!

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">

我在做什么错了?

推荐答案

要遵循的步骤:

将meta标记用于UTF8.

Use the meta tag for UTF8.

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

将您的PHP设置为使用UTF8.

Set your PHP to use UTF8.

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

对于mysql,您要将表转换为UTF8.

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

ALTER TABLE table_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci

还可以运行:

 SET NAMES UTF8

作为建立连接后的第一个查询,它将把您的数据库连接转换为UTF8.

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

这篇关于php/mysql-外来字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆