MySQL 或 PHP 附加一个 ? 每当使用 £ 时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/386378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL or PHP is appending a ? whenever the £ is used
提问by suitedupgeek
Answers provided have all been great, I mentioned in the comments of Alnitak's answer that I would need to go take a look at my CSV Generation script because for whatever reason it wasn't outputting UTF-8.
提供的答案都很棒,我在 Alnitak 的答案的评论中提到,我需要查看我的 CSV 生成脚本,因为无论出于何种原因,它都没有输出 UTF-8。
As was correctly pointed out, it WAS outputting UTF-8 - the problem existed with Ye Olde Microsoft Excel which wasn't picking up the encoding the way I would have liked.
正如正确指出的那样,它输出的是 UTF-8 - Ye Olde Microsoft Excel 存在的问题,它没有按照我希望的方式获取编码。
My existing CSV generation looked something like:
我现有的 CSV 生成看起来像:
// Create file and exit;
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
echo $csv_output;
It now looks like:
现在看起来像:
// Create file and exit;
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: text/csv; charset=ISO-8859-1");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");
echo iconv('UTF-8', 'ISO-8859-1', $csv_output);
-------------------------------------------------------
-------------------------------------------------- -----
ORIGINAL QUESTION
原问题
Hi,
你好,
I've got a form which collects data, form works ok but I've just noticed that if someone types or uses a '£' symbol, the MySQL DB ends up with '?£'.
我有一个收集数据的表单,表单可以正常工作,但我刚刚注意到,如果有人输入或使用“£”符号,MySQL 数据库会以“?£”结尾。
Not really sure where or how to stop this from happening, code and DB information to follow:
不确定在何处或如何阻止这种情况发生,要遵循的代码和数据库信息:
MySQL details
MySQL 详细信息
mysql> SHOW COLUMNS FROM fraud_report;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | mediumint(9) | | PRI | NULL | auto_increment |
| crm_number | varchar(32) | YES | | NULL | |
| datacash_ref | varchar(32) | YES | | NULL | |
| amount | varchar(32) | YES | | NULL | |
| sales_date | varchar(32) | YES | | NULL | |
| domain | varchar(32) | YES | | NULL | |
| date_added | datetime | YES | | NULL | |
| agent_added | varchar(32) | YES | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
8 rows in set (0.03 sec)
PHP Function
PHP函数
function processFraudForm($crm_number, $datacash_ref, $amount, $sales_date, $domain, $agent_added) {
// Insert Data to DB
$sql = "INSERT INTO fraud_report (id, crm_number, datacash_ref, amount, sales_date, domain, date_added, agent_added) VALUES (NULL, '$crm_number', '$datacash_ref', '$amount', '$sales_date', '$domain', NOW(), '$agent_added')";
$result = mysql_query($sql) or die (mysql_error());
if ($result) {
$outcome = "<div id=\"success\">Emails sent and database updated.</div>";
} else {
$outcome = "<div id=\"error\">Something went wrong!</div>";
}
return $outcome;
}
Example DB Entry
示例数据库条目
+----+------------+--------------+---------+------------+--------------------+---------------------+------------------+
| id | crm_number | datacash_ref | amount | sales_date | domain | date_added | agent_added |
+----+------------+--------------+---------+------------+--------------------+---------------------+------------------+
| 13 | 100xxxxxxx | 10000000 | ?£10.93 | 18/12/08 | blargh.com | 2008-12-22 10:53:53 | agent.name |
回答by Alnitak
What you're seeing is UTF-8encoding - it's a way of storing Unicode characters in a relatively compact format.
您所看到的是UTF-8编码 - 这是一种以相对紧凑的格式存储 Unicode 字符的方式。
The pound symbol has value 0x00a3in Unicode, but when it's written in UTF-8 that becomes 0xc2 0xa3and that's what's stored in the database. It seems that your database table is already set to use UTF-8 encoding. This is a good thing!
英镑符号0x00a3在 Unicode 中具有价值,但是当它以 UTF-8 编写时,它就会变成0xc2 0xa3并且这就是存储在数据库中的内容。您的数据库表似乎已设置为使用 UTF-8 编码。这是一件好事!
If you pull the value back out from the database and display it on a UTF-8 compatible terminal (or on a web page that's declared as being UTF-8 encoded) it will look like a normal pound sign again.
如果您从数据库中取出该值并将其显示在 UTF-8 兼容终端上(或在声明为 UTF-8 编码的网页上),它将再次看起来像一个正常的英镑符号。
回答by Paul Dixon
?£ is 0xC2 0xA3 which is the UTF-8 encoding for £ symbol - so you're storing it as UTF-8, but presumably viewing it as Latin-1 or something other than UTF-8
?£ 是 0xC2 0xA3,它是 £ 符号的 UTF-8 编码 - 因此您将其存储为 UTF-8,但大概将其视为 Latin-1 或 UTF-8 以外的其他内容
It's useful to know how to spot and decode UTF-8 by hand - check the wikipedia pagefor info on how the encoding works:
了解如何手动识别和解码 UTF-8 很有用 - 检查维基百科页面以获取有关编码如何工作的信息:
- 0xC2A3 = 110 0001010 100011
- The bold parts are the actual "payload", which gives 10100011, which is 0xA3, the pound symbol.
- 0xC2A3 = 110 0001010 100011
- 粗体部分是实际的“有效载荷”,它给出了 10100011,即 0xA3,即英镑符号。
回答by Ben
In PHP, another small scale solution is to do a string conversion on the returned utf8 string:
在 PHP 中,另一个小规模的解决方案是对返回的 utf8 字符串进行字符串转换:
print iconv('UTF-8', 'ASCII//TRANSLIT', "Mystring a"); //"Mystring "
Or in other platforms fire a system call to the inconv command (linux / osx)
或者在其他平台上触发对 inconv 命令的系统调用(linux / osx)
回答by Ben
You need to serve your HTML in utf-8 encoding (actually everyone needs to do this I think!) Header like:
您需要以 utf-8 编码提供 HTML(我认为实际上每个人都需要这样做!)标题如下:
Content-Type: text/html; charset=UTF-8
内容类型:文本/html;字符集=UTF-8
Or the equivalent. Double check the details though. Should always be declaring the charset as a browser can default to anything it likes.
或等价物。仔细检查细节。应该始终声明字符集,因为浏览器可以默认为它喜欢的任何内容。
回答by user109764
To remove a ? use:
删除一个 ? 用:
$column = str_replace("\xc2\xa0", '', $column);
$column = str_replace("\xc2\xa0", '', $column);
Credits among others: How to remove all occurrences of c2a0 in a string with PHP?
回答by user109764
Thanks a lot. I had been suspecting mysql for being currupting the pound symbol. Now all i need to do is wherever the csv record is generated, just use wrap them incov funciton. Though this is a good job, I am happy, at least someone showed exactly what to do. I sincerly appreciate dislaying the previous and the new 'header' values. It was a great help to me.
非常感谢。我一直怀疑 mysql 破坏了英镑符号。现在我需要做的就是在生成 csv 记录的任何地方,只需使用 wrap them incov 函数。虽然这是一份好工作,但我很高兴,至少有人表明了该做什么。我真诚地感谢显示以前的和新的“标题”值。这对我帮助很大。
-mark
-标记
回答by Sunil Rajput
If you save line "The £50,000 Development Challenge" in two different data type column i.e. "varchar" & "text" field.
如果将“The £50,000 Development Challenge”行保存在两个不同数据类型的列中,即“varchar”和“text”字段。
Before i save i have replaced the symbol with html equi value using following function. str_replace("£", "£", $title);
在我保存之前,我已经使用以下函数用 html equi 值替换了符号。str_replace("£", "£", $title);
You will find that value stored in text fields is £ where as in varchar its "?£".
您会发现文本字段中存储的值是 £,而 varchar 中的值是“?£”。

