如何解决将SQL中的表情符号的String Unicode表示形式转换为Dart或PHP中的实际表情符号
我整天整夜都在此上度过。来自许多人的许多答案,但没有一个适合我的情况。对不起,这个问题很抱歉,但是我昨天整个时间都花在了这上面,直到清晨。
所以我有一个旧系统,可以迁移到新系统。 本质上是从SQL Server到Firebase。采用新的结构,所有新的uid和客户端都希望将旧SQL服务器中的unicode数据转换为真实的表情符号。
因此,它来自SQL(在旧服务器中)=> PHP(在旧服务器中以获取SQL数据)=> Flutter(在我的笔记本电脑中)=> Firebase(Firestore)。
在SQL Server中,有这样的字符串,例如:Spring... fall...\ud83d\udc90...flowers!
然后从SQL数据库检索的PHP代码将其返回,如下所示:
// Create Connection
$conn = new mysqli($servername,$username,$password,$dbname);
mysqli_set_charset($conn,'utf8mb4');
header("Access-Control-Allow-Origin: *");
$result = $conn->query($sql);
if($result->num_rows > 0) {
$db_data = array();
while($row = $result->fetch_assoc()) {
$db_data[] = $row;
}
// Send back the complete records as a json
echo json_encode($db_data,JSON_UNESCAPED_UNICODE);
}else{
http_response_code(405);
echo "Query error";
}
$result->free();
$conn->close();
return;
在Dart(Flutter网站)中,我得到了这样的数据:
final response = await http.get(urlRequest);
final rawRows = jsonDecode(response.body) as List<dynamic>;
rawRows.forEach((map) => print(map['description']));
打印输出:Spring... fall...\ud83d\udc90...flowers!
我想要的是Spring... fall...?...flowers!
我进行了硬编码的打印测试(即print('Spring... fall...\ud83d\udc90...flowers!');
,它给出的结果恰好是我想要的(即:将字符串的\ud83d\udc90
的一部分变成了?)
我尝试将其原样发送到Firebase,但仍无法将\ud83d\udc90
识别为表情符号。它像这样Spring... fall...\ud83d\udc90...flowers!
一样存储在Firebase中。完全可以理解,我想我们需要先对其进行转换。
我也曾尝试在PHP方面使用转换器,例如:(thanks to Pedro Lobito)
function unicodeString($str,$encoding=null) {
if (is_null($encoding)) $encoding = ini_get('mbstring.internal_encoding');
return preg_replace_callback('/\\\\u([0-9a-fA-F]{4})/u',create_function('$match','return mb_convert_encoding(pack("H*",$match[1]),'.var_export($encoding,true).',"UTF-16BE");'),$str);
}
然后将PHP中的$db_data[] = $row;
部分更改为$db_data[] = unicodeString($row);
但是此解决方案仅适用于某些表情符号,而不适用于所有表情符号,例如上述?的情况。而是显示Spring... fall...?...flowers!
。
所以现在我想尝试将其转换为Dart,因为我对此比较熟悉。除非有人也可以用PHP解决该问题。
解决方法
最后工作几天后,我研究了自己的解决方案。
所以我要做的是使用RegEx
检测字符串中的序列并将其替换为表情符号。
- 声明用于检测unicode的正则表达式(在我的情况下,unicode类似于(\ uXXX):
// To detect a single unicode
static const regEx1 = '\\\\u([0-9a-fA-F]{4})';
// To detect a 2-bytes unicode
static const regEx2 = '$regEx1$regEx1';
- 创建一种方法来检测每个RegEx。然后,在找到模式时,将其替换为
jsonDecode
,并在其中添加引号。像这样jsonDecode('"${match.group(0)}"');
。
代码:
static String _regExEmojiUnicode(String text,String regEx) {
final regexCheck = RegExp(regEx,caseSensitive: false);
String newText = '';
int _lastEndText = 0;
int _lastEndNewText = 0;
regexCheck.allMatches(text).forEach((match) {
final start = match.start;
final end = match.end;
final String replacement = jsonDecode('"${match.group(0)}"');
String startString;
newText == ''
? startString = '${text.substring(0,start)}$replacement'
: startString =
'${newText.substring(0,_lastEndNewText)}${text.substring(_lastEndText,start)}$replacement';
_lastEndNewText = startString.length;
_lastEndText = end;
newText =
'$startString${text.substring(end)}';
});
if(newText == '') newText = text;
return newText;
}
- 创建一种方法来检查表情符号的不同情况:
static String replaceEmoji(String text) {
String newText = text;
// Checking for 2-bytes and single bytes emojis
if(newText.contains('\\u'))
newText = _regExEmojiUnicode(newText,regEx2);
if(newText.contains('\\u'))
newText = _regExEmojiUnicode(newText,regEx1);
return newText;
}
那么这行得通!
我匆忙进行了此操作,因此,如果可以对RegEx模式或代码中的其他任何地方进行改进,则我欢迎任何建议。
谢谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。