本文目录导读:

- iconv 函数(最常用)
- mb_convert_encoding 函数(多字节安全)
- iconv 与 mb_convert_encoding 对比
- 其他相关函数
- 综合示例:智能转换函数
- 注意事项
- 常用编码转换示例
- 编码检测函数
在PHP中,字符集转换是一个常见的需求,特别是在处理多语言网站或与不同系统交互时,以下是PHP中常用的字符集转换函数及方法:
iconv 函数(最常用)
// 基本语法
iconv(string $from_encoding, string $to_encoding, string $string): string|false
// 示例:UTF-8 转 GBK
$utf8_string = "你好,世界";
$gbk_string = iconv("UTF-8", "GBK", $utf8_string);
// 添加 //TRANSLIT 或 //IGNORE 处理特殊字符
$result = iconv("UTF-8", "GBK//IGNORE", $string);
$result = iconv("UTF-8", "GBK//TRANSLIT", $string);
mb_convert_encoding 函数(多字节安全)
// 基本语法 mb_convert_encoding(string $string, string $to_encoding, mixed $from_encoding = null): string|false // 示例 $result = mb_convert_encoding($string, "UTF-8", "GBK"); $result = mb_convert_encoding($string, "UTF-8", "auto"); // 自动检测 // 多个源编码 $result = mb_convert_encoding($string, "UTF-8", "UTF-8,GBK,GB2312");
iconv 与 mb_convert_encoding 对比
// iconv 特点:
// - 速度快
// - 不支持自动检测编码
// - 对某些字符处理可能出错
// mb_convert_encoding 特点:
// - 更灵活,支持自动检测
// - 支持多字节字符处理
// - 需要 mbstring 扩展
// 使用场景示例
function convertEncoding($string, $to, $from = "auto") {
if (function_exists('mb_convert_encoding')) {
return mb_convert_encoding($string, $to, $from);
} else {
return iconv($from, $to, $string);
}
}
其他相关函数
// 编码检测 $encoding = mb_detect_encoding($string, ['UTF-8', 'GBK', 'ASCII']); $is_utf8 = mb_check_encoding($string, 'UTF-8'); // 获取系统支持的编码 $encodings = mb_list_encodings(); // UTF-8 编码处理 $utf8_string = utf8_encode($string); // ISO-8859-1 转 UTF-8 $iso_string = utf8_decode($utf8_string); // UTF-8 转 ISO-8859-1
综合示例:智能转换函数
/**
* 智能字符集转换函数
*/
function smartConvert($string, $to = 'UTF-8') {
if (empty($string)) return '';
// 如果已是目标编码,直接返回
if (mb_check_encoding($string, $to)) {
return $string;
}
// 尝试检测源编码
$from = mb_detect_encoding($string, [
'UTF-8', 'GBK', 'GB2312', 'BIG5',
'ISO-8859-1', 'ASCII', 'EUC-JP', 'SJIS'
]);
if ($from && $from != $to) {
return mb_convert_encoding($string, $to, $from);
}
// 尝试自动转换
return mb_convert_encoding($string, $to, 'auto');
}
// 使用示例
$text = "中文测试";
echo smartConvert($text, 'UTF-8');
echo smartConvert($text, 'GBK');
注意事项
// 1. 确保 PHP 配置支持
// 检查 iconv 和 mbstring 扩展
if (!extension_loaded('mbstring')) {
echo "mbstring 扩展未安装";
}
// 2. 错误处理
$converted = @iconv("UTF-8", "GBK", $string);
if ($converted === false) {
echo "转换失败";
}
// 3. 处理大文件
function convertLargeFile($inputFile, $outputFile, $from, $to) {
$handle = fopen($inputFile, 'r');
$output = fopen($outputFile, 'w');
while (!feof($handle)) {
$chunk = fread($handle, 8192);
if ($chunk !== false) {
$converted = mb_convert_encoding($chunk, $to, $from);
fwrite($output, $converted);
}
}
fclose($handle);
fclose($output);
}
常用编码转换示例
// UTF-8 转 URL 编码
$url_string = rawurlencode("中文");
// 输出: %E4%B8%AD%E6%96%87
// HTML 实体转换
$html_string = htmlentities("中文", ENT_COMPAT, "UTF-8");
// 输出: 中文
// 数据库连接编码设置
mysqli_set_charset($connection, 'utf8mb4');
编码检测函数
/**
* 检测并输出字符串的编码信息
*/
function detectEncoding($string) {
$encodings = [
'UTF-8', 'GBK', 'GB2312', 'BIG5',
'ISO-8859-1', 'Windows-1252', 'ASCII'
];
$detected = mb_detect_encoding($string, $encodings, true);
return [
'encoding' => $detected,
'is_valid_utf8' => mb_check_encoding($string, 'UTF-8'),
'length' => mb_strlen($string)
];
}
选择使用哪种函数取决于你的具体需求,通常建议使用 mb_convert_encoding,因为它更灵活且支持自动编码检测,对于追求性能的场景,iconv 可能是更好的选择。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播