简体   繁体   English

提交表格中的西班牙口音标记

[英]Spanish Accent Marks in Form Submission

I have a form where spanish can be submitted and using PHP, I send an email with the data. 我有一个表格可以提交西班牙语并使用PHP,我发送一封包含数据的电子邮件。 Unfortunately, the accent marks get totally screwed up when they get emailed. 不幸的是,当他们收到电子邮件时,重音标记完全搞砸了。

If I submit the following: 如果我提交以下内容:

Testing Accent Marks á é í ó ú ñ 测试口音标记áéíóú

I end up with the following in the body of my email... 我最终在我的电子邮件正文中有以下内容......

Testing Accent Marks á é à ó ú ñ 测试重音标记áÃÃÃÃÃÃÃÃâ

The code that is processing the email is simply placing the $_POST info directly into the body of the email. 处理电子邮件的代码只是将$ _POST信息直接放入电子邮件正文中。 I assume I need to have htmlentities() or something but I have tried and nothing works... 我认为我需要有htmlentities()或者其他东西,但我已经尝试过但没有任何作用......

I also will need to be placing the same data into a MySQL database and retrieving it later. 我还需要将相同的数据放入MySQL数据库并稍后检索。 What do I need to be aware of when I do that? 当我这样做时,我需要注意什么?

Thanks! 谢谢! Drew 德鲁

You have touched the fine subject of charsets. 你已经触及了charsets的精细主题。 Try to create and convert everything to utf-8. 尝试创建并将所有内容转换为utf-8。 Database, files, forms and the like. 数据库,文件,表格等。 Look up some information on the internet about what header to use in the e-mail to make it utf-8. 在互联网上查找有关在电子邮件中使用哪个标头以使其成为utf-8的一些信息。 Also convert you e-mails from standard 7-bit to 8-bit with these headers. 还可以使用这些标头将您的电子邮件从标准的7位转换为8位。

"Content-Type: text/plain; charset=utf-8"
"Content-Transfer-Encoding: 8bit"

For the database you need to set, in case of mysql, the collation. 对于数据库,您需要在mysql的情况下设置排序规则。

To answer your question about storing data in the database: 要回答有关在数据库中存储数据的问题:

After you have opened a connection to MySQL using mysql_connect(), send the following query as your first command: 使用mysql_connect()打开与MySQL的连接后,将以下查询作为第一个命令发送:

mysql_query("SET NAMES utf8");

(or mysqli_query(), depending on the library you use). (或mysqli_query(),具体取决于您使用的库)。

This command lets MySQL database know that: 这个命令让MySQL数据库知道:

  • all data you'll be providing will be encoded as UTF-8, and 您将提供的所有数据将编码为UTF-8,和
  • you also want to receive everything in UTF-8. 你也想收到UTF-8的所有东西。

Strictly speaking, the database itself can be in whatever collation and encoding (MySQL is happy to transcode stuff for you on the fly), but of course, it's best to have the database in UTF-8 as well. 严格来说,数据库本身可以采用任何排序和编码方式(MySQL很乐意为您动态转码),但当然,最好还是使用UTF-8数据库。

If you're using utf8 subjects, you need to base64 encode them as such first: 如果你正在使用utf8主题,你需要首先对它们进行base64编码:

$subject = "=?utf-8?b?".base64_encode($subject)."?=";

And set the content type as such: 并设置内容类型:

$headers.="Content-Type: text/html; charset=utf-8\r\n";

I also recommend you use a system like SwiftMailer to prevent header injection attacks. 我还建议您使用类似SwiftMailer的系统来防止标头注入攻击。 For the database part, I believe that would be better off as a separate question to get proper tags and more focused responses. 对于数据库部分,我认为作为一个单独的问题,获得适当的标签和更集中的响应会更好。

Your script is receiving data encoded in UTF-8, but your email client believes that the email is encoded in Latin-1. 您的脚本正在接收以UTF-8编码的数据,但您的电子邮件客户端认为该电子邮件是使用Latin-1编码的。 There are two ways you might deal with this: add a header to the email indicating that the character encoding is UTF-8, or use utf8_decode() on the submitted content (if you are sure you will only ever use Latin-1 characters). 有两种方法可以解决这个问题:在电子邮件中添加一个标题,表示字符编码为UTF-8,或者在提交的内容上使用utf8_decode() (如果您确定只使用Latin-1字符) 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM