简体   繁体   English

PHP中的字符编码错误

[英]Character encoding error in Php

Hi I am storing some text in a mysql column. 嗨,我在mysql列中存储一些文本。 The error I am getting is whenever there is an apostrophe (') in the text it's getting displayed as '’'even though apostrophe is being displayed in the database Any ideas what would be the error? 我遇到的错误是,即使数据库中显示了撇号,只要文本中出现撇号('),它就会显示为“—。任何想法是什么错误? I have spent days but no luck 我花了几天但没有运气

You can check the error here : http://beta.writiely.com/ 您可以在此处检查错误: http : //beta.writiely.com/

Here how it's connecting and fetching result 这里是如何连接和获取结果的

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * from textDocs";
$result = $conn->query($sql);

$res = array();
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
array_push($res,array('t'=>$row['t'],'co'=>$row['cover'],
     'text'=>$row['txt']));
    }
} else {
    return false;
}
$conn->close();

’ is the Mojibake for the utf8 RIGHT SINGLE QUOTATION MARK , which is not normally used in computer circles. ’是变为乱码的UTF8 RIGHT SINGLE QUOTATION MARK ,这通常不在计算机界使用。 (You probably got it from some Word processing package.) (您可能是从某些Word处理包中获得的。)

htmlentities should not be relevant to this discussion. htmlentities不应该与此讨论相关。 If the web page has ’ 如果网页上有’ , you have a big mess. ,您一团糟。 I don't think it has ’ 我认为它没有’ based on what you have said. 根据您所说的

Yes, $conn->set_charset("utf8"); 是的, $conn->set_charset("utf8"); is needed. 是必需的。 And this is usually the fix for this type of problem. 这通常是解决此类问题的方法。

However, you also need the table/column to be CHARACTER SET utf8 , is it? 但是,您还需要将表/列设置为CHARACTER SET utf8 ,对吗? Do SHOW CREATE TABLE to check. 执行SHOW CREATE TABLE进行检查。

utf8 and utf8mb4 do not differ in this context. utf8utf8mb4在这方面没有区别。 The latter is needed for Emoji and some of Chinese. 表情符号和某些中文需要后者。

The "collation" (such as utf8_general_ci ) is not relevant to this discussion. “排序规则”(例如utf8_general_ci )与此讨论无关。 However, utf8_general_ci applies only to utf8 , so some of the comments were unambiguous. 然而, utf8_general_ci 适用于utf8 ,所以一些评论是毫不含糊的。

Summary: 摘要:

  • Your bytes are apparently utf8, 您的字节显然是utf8,
  • Do the set_charset , set_charset
  • Check the table/column, 检查表/列
  • HTML4 needs <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> ; HTML4需要<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> HTML5 can shorten it: <meta charset="UTF-8"> (but apparently this is not the issue). HTML5可以缩短它: <meta charset="UTF-8"> (但显然这不是问题)。

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

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