如何在Php中显示sql中的德国元音(ü,ä,ö)?

时间:2012-07-11 11:52:05

标签: php character-encoding

我试图以正确的方式显示这个德语字符,如果我不使用htmlentities()输出unabh ngig,但是当我尝试使用{{1}时这个词不见了,为什么会这样?

htmlentities()

4 个答案:

答案 0 :(得分:7)

我和ü,ä等有同样的问题。

选择数据库后,我实现了这两行:

mysql_query("SET NAMES 'utf8'"); 
mysql_query("SET CHARACTER SET 'utf8'");

如下所示: http://www.flashhilfe.de/forum/serverseitige-programmierung/php-mysql-wie-utf-8-beim-connect-festlegen-225225-225225.html

Best,Yannis

答案 1 :(得分:0)

你应该能够通过确保使用HTTP标头将字符集设置为utf-8来发送ü而不单独编码。

如果由于某种原因你不能这样做,请使用:

Ü = Ü

ü = ü

答案 2 :(得分:0)

这是charset的格式:

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

确保htaccess或apache不强制您的编码。在查看站点时检查浏览器中选择的编码是什么,如果不是utf-8,则会强制它。如果强迫你可以将其添加到htaccess

AddDefaultCharset UTF-8

确保您的数据库表格为utf-8

确保您与数据库的连接位于utf-8 http://php.net/manual/en/function.mysql-set-charset.php

mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $conn);

您可以将UTF-8编码传递给htmlentities,以使其使用正确的编码。或者您可以使用htmlspecialchars http://php.net/manual/en/function.htmlentities.php

答案 3 :(得分:0)

如果您不想担心这么多不同的字符集编码或者如果不适合您,那么可以选择以下方法: 我使用mysqli DB连接(和PHPV5)表单发送/插入MySQl DB。

$Notes = $_POST['Notes']; //can be text input or textarea.

$Notes = str_replace('"', '&quot;', $Notes);  //double quotes for mailto: emails.  
$von = array("ä","ö","ü","ß","Ä","Ö","Ü"," ","é");  //to correct double whitepaces as well
$zu  = array("&auml;","&ouml;","&uuml;","&szlig;","&Auml;","&Ouml;","&Uuml;","&nbsp;","&#233;");  
$Notes = str_replace($von, $zu, $Notes);  
echo " Notes:".$Notes."<br>" ;  
$Notes = mysqli_real_escape_string($link, $Notes); //for recommended mysqli DB connection.
//$Notes = mysql_real_escape_string($Notes); //for old deprecated mysql DB connection.

// mysqli_real_escape_string Escapes special characters in a string for use in an SQL statement

echo " Notes:".$Notes ;  //ready for inserting
enter code here
//Optional:     
$charset = mysqli_character_set_name($link);  //only works for mysqli DB connection
printf ("To check your character set but not necessary %s\n",$charset);