先安装库
# Ubuntu/Debian
apt-get install opencc
# CentOS
yum install opencc
自己看是什么服务器,然后运行指令:
opencc -i db_backup.sql -o db_traditional.sql -c s2t.json
其中
-i db_backup.sql:input 输入文件,你的原始简体SQL文件-o db_traditional.sql:output 输出文件,转换后的繁体SQL文件-c s2t.json:config 转换配置,s2t = Simplified to Traditional(简体转繁体)
这样就能得到一个全部汉字都转换过得了.如果你想单独转换某个表里面的字段,则需要先读取表字段再转换,以php为例:
需要开启shell_exec函数,用完记得关闭.代码如下 private function convertToTraditionalChinese($text) { // 添加调试信息 // echo "PHP OpenCC扩展: " . (class_exists('\\Naux\\OpenCC') ? "可用" : "不可用") . "\n"; // echo "系统OpenCC命令: " . ($this->isOpenCCAvailable() ? "可用" : "不可用") . "\n"; // echo "shell_exec函数: " . (function_exists('shell_exec') && is_callable('shell_exec') ? "可用" : "不可用") . "\n"; // dd(1); // 尝试使用PHP OpenCC扩展 if (class_exists('\\Naux\\OpenCC')) { try { $opencc = new \Naux\OpenCC(); return $opencc->convert($text, 's2t'); // 简体到繁体 } catch (\Exception $e) { // 记录错误日志但继续尝试其他方法 error_log("OpenCC extension error: " . $e->getMessage()); } } // 尝试使用系统命令行工具 if (function_exists('shell_exec') && is_callable('shell_exec') && $this->isOpenCCAvailable()) { return $this->convertWithSystemOpenCC($text); } return '不可用'; }你也可以选择直接安装php扩展,注意php版本,推荐74.