7

我试图以正确的方式显示这个德语字符,如果我不使用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>
4

4 回答 4

8

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

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

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

最好的,雅尼斯

于 2013-03-31T18:48:57.100 回答
0

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

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

Ü = &Uuml;

或者

ü = &uuml;
于 2012-07-11T11:54:59.837 回答
0

如果你不想担心这么多不同的字符集编码,或者如果 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);  
于 2014-01-02T13:09:50.517 回答
0

这是字符集的格式:

<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

于 2012-07-11T12:01:51.733 回答