我正在为网站使用繁荣库。我的客户要求我们能够在手机上使用表情符号。理论上我们应该将MySQL数据库的字符编码从utf8更改为utf8mb4。
到目前为止,一切都很好,但是,如果我们进行此切换,如下所示:
# For each database:
ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE utf8mb4_unicode_ci;
# For each table:
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
# For each column:
ALTER TABLE table_name CHANGE column_name column_name VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
# (Don’t blindly copy-paste this! The exact statement depends on the column type, maximum length, and other properties. The above line is just an example for a `VARCHAR` column.)
那么每个字符将使用四个字节而不是三个字节。这将使数据库的大小增加 33%。这会导致性能变差并占用更多存储空间。因此,我们决定仅针对特定表的特定列切换到 utf8mb4 编码。
为了确保一切正常,我检查了几件事。其中,我检查了flushlib,发现了几个可疑的部分:
有一个fUTF8类,好像不支持utf8mb4
在 fDatabase,我引用了一些发现:
if ($this->connection && function_exists('mysql_set_charset') && !mysql_set_charset('utf8', $this->connection)) { throw new fConnectivityException( 'There was an error setting the database connection to use UTF-8' ); } //... // Make MySQL act more strict and use UTF-8 if ($this->type == 'mysql') { $this->execute("SET SQL_MODE = 'REAL_AS_FLOAT,PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE'"); $this->execute("SET NAMES 'utf8'"); $this->execute("SET CHARACTER SET utf8"); }
在 fSQLSchemaTranslation 中我可以看到:
$sql = preg_replace('#\)\s*;?\s*$#D', ')ENGINE=InnoDB, CHARACTER SET utf8', $sql);
我怀疑繁荣库不会支持我们使几个表的几列具有utf8mb4字符编码的追求。我想知道我们是否可以以某种方式升级一些东西来提供这种支持。最坏的情况是,我们可以将每个出现的 utf8 文本覆盖为 utf8mb4。然而,这将是一个非常丑陋的黑客行为,我们想知道是否有更好的解决方案。我们应该进行这种破解还是有更正统的方法?
最佳答案
我已经解决了这个问题。我通过更改列字符集和排序规则来更改要支持表情符号的表,如下所示:
ALTER TABLE table_name CHANGE column_name column_name text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
在那之后,我不得不做一些丑陋的修改来让繁荣库能够支持表情符号。
fDatabase.php:
第 685 行:
if ($this->connection && function_exists('mysql_set_charset') && !mysql_set_charset('utf8mb4', $this->connection)) {
throw new fConnectivityException(
'There was an error setting the database connection to use UTF-8'
);
}
第 717 行保持不变,如果更改此行,一切都会崩溃:
if ($this->connection && function_exists('mysqli_set_charset') && !mysqli_set_charset($this->connection, 'utf8')) {
第 800 行:
// Make MySQL act more strict and use UTF-8
if ($this->type == 'mysql') {
$this->execute("SET SQL_MODE = 'REAL_AS_FLOAT,PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE'");
$this->execute("SET NAMES 'utf8mb4'");
$this->execute("SET CHARACTER SET utf8mb4");
}
fSQLSchemaTranslation.php:
第 1554 行:
$sql = preg_replace('#\)\s*;?\s*$#D', ')ENGINE=InnoDB, CHARACTER SET utf8mb4', $sql);
fXML.php:
第 403 行:
if (preg_replace('#[^a-z0-9]#', '', strtolower($encoding)) == 'utf8mb4') {
// Remove the UTF-8 BOM if present
$xml = preg_replace("#^\xEF\xBB\xBF#", '', $xml);
fCore::startErrorCapture(E_NOTICE);
$cleaned = self::iconv('UTF-8', 'UTF-8', $xml);
if ($cleaned != $xml) {
$xml = self::iconv('Windows-1252', 'UTF-8', $xml);
}
fCore::stopErrorCapture();
}
最后,当任何受影响的列发生修改时,我执行以下命令:
App::db()->query("set names 'utf8mb4'");
这实际上会触发 fDatabase
对象的 ->query()
执行。
关于php - 如何支持表情符号蓬勃发展?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32446440/