我有一个非常奇怪的现象,试图将一个旧的php项目从iso-8859-1迁移到utf-8
当在文本字段中键入像"äüß"这样的德语元音变音符并提交时,我会收到像"�"
标头设置为
<?php header("Content-Type:text/html;charset=UTF-8"); ?>
<meta charset="UTF-8" />
表单设置为
<form accept-charset="utf-8">
等等——我想尽一切办法——有人对类似的问题有经验吗?
致以最良好的问候,Lupo
我找到了一个适用于我的案例的解决方案:
我不得不将accept字符集从UTF-8改回ISO-8859-15,然后字段值看起来又正常了——为了能够处理接收到的数据,我必须对它们进行utf8_encode——这看起来有点奇怪,但现在它工作得很好。
感谢大家抽出时间和Jukka的帮助。
致以最良好的问候,Lupo
我很高兴您找到了解决方案。如果你不想担心这么多不同的字符集编码,或者htmlentities对其他人不起作用,这里有另一种选择:我使用了mysqli DB连接(和PHPV5)Form post来编写/插入MySQl-DB。
$Notes = $_POST['Notes']; //can be text input or textarea.
$Notes = str_replace('"', '"', $Notes); //double quotes for mailto: emails.
$von = array("ä","ö","ü","ß","Ä","Ö","Ü"," ","é"); //to correct double whitepaces as well
$zu = array("ä","ö","ü","ß","Ä","Ö","Ü"," ","é");
$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
//Optinal:
$charset=mysqli_character_set_name($link)//仅适用于mysqli数据库连接printf("检查您的字符集,但不是必需的%s''n",$charset);