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


How to display Germany Umlauts (ü, ä, ö) from sql in Php?

我正在尝试以正确的方式显示此德语字符,如果我不使用htmlentities(),则输出为unabh�ngig,但当我尝试使用htmlentities()时,这个词不见了,为什么?

<?php
$str = htmlentities("unabhängig");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
<meta charset='utf-8'>
</head>
<body>
</body>
<div><?php echo $str;?></div>

我对ü、ä等也有同样的问题

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

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

最佳,Yannis

通过确保使用HTTP头将字符集设置为utf-8,您应该能够在不单独编码的情况下发送ü。

如果由于某些原因您无法做到这一点,请使用:

Ü = &Uuml;

ü = &uuml;

这是字符集的格式:

<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,使其使用正确的编码。或者您可以使用htmlspecialcharshttp://php.net/manual/en/function.htmlentities.php

如果你不想担心这么多不同的字符集编码,或者如果htmlentities不适合你,这里有另一种选择:我使用了mysqli DB连接(和PHPV5)Form post来编写/插入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);