function custom_encrypt($string) { $string = mb_convert_encoding($string, 'UTF-8', 'auto'); $base64 = base64_encode($string); $url_safe = str_replace(['+', '/', '='], ['-', '_', ''], $base64); return strtolower($url_safe); } function custom_decrypt($string) { $base64 = str_replace(['-', '_'], ['+', '/'], $string); $padding = strlen($base64) % 4; if ($padding > 0) { $base64 .= str_repeat('=', 4 - $padding); } $result = base64_decode($base64); return mb_convert_encoding($result, 'UTF-8', 'auto'); } $s = '2_中文'; $encrypted = custom_encrypt($s); echo "加密后的字符串: $encrypted\n"; $decrypted = custom_decrypt($encrypted); echo "解密后的字符串: $decrypted\n";