From f537a2c4a410e430b621674753d3df4c0e6ef1c0 Mon Sep 17 00:00:00 2001 From: ASUS <515617283@qq.com> Date: Mon, 20 Oct 2025 19:08:45 +0800 Subject: [PATCH] =?UTF-8?q?=E7=89=B9=E6=AE=8A=E5=AD=97=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/functions.php | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/app/functions.php b/app/functions.php index b12922c..59a2ab0 100644 --- a/app/functions.php +++ b/app/functions.php @@ -148,7 +148,7 @@ if (!function_exists('saveJson_arr')) { foreach ($cont_json_arr as $k1 => $v1) { if($k == $v1) { if($k1 == 'username') { - $new_arr[$k1] = $user['nick_name']; + $new_arr[$k1] = removeEmojiAndSpecialChars($user['nick_name'], false); }else { $new_arr[$k1] = $v; } @@ -201,3 +201,38 @@ if (!function_exists('replace_json_keys')) { } } + +/** + * 移除字符串中的表情和特殊字符 + * @param string $str 原始字符串 + * @param bool $keepBasicPunct 是否保留基本标点(默认保留) + * @return string 处理后的字符串 + */ +if (!function_exists('removeEmojiAndSpecialChars')) { + function removeEmojiAndSpecialChars($str, $keepBasicPunct = true) + { + // 1. 移除 Emoji 表情(匹配常见 Emoji 的 Unicode 范围) + $emojiPattern = '/[\x{1F600}-\x{1F64F}]|[\x{1F300}-\x{1F5FF}]|[\x{1F680}-\x{1F6FF}]|[\x{1F1E0}-\x{1F1FF}]|[\x{2600}-\x{26FF}]/u'; + $str = preg_replace($emojiPattern, '', $str); + + // 2. 移除特殊字符(控制字符、非预期字符) + if ($keepBasicPunct) { + // 保留:字母、数字、中文、空格,以及基本标点(!@#$%^&*()_+-= etc.) + // 正则含义:匹配所有不在以下范围内的字符并移除 + // \p{L}:所有语言的字母(包括中文、英文、日文等) + // \p{N}:所有数字 + // \s:空白字符(空格、换行等) + // !-~:ASCII 可见标点(33-126 范围内的字符) + $specialPattern = '/[^\p{L}\p{N}\s!-~]/u'; + } else { + // 不保留标点:只保留字母、数字、中文、空格 + $specialPattern = '/[^\p{L}\p{N}\s]/u'; + } + $str = preg_replace($specialPattern, '', $str); + + // 3. 移除连续的空白字符(可选,根据需求) + $str = preg_replace('/\s+/', ' ', $str); + + return trim($str); + } +}