{"id":17922686,"url":"https://github.com/overtrue/php-opencc","last_synced_at":"2025-08-13T14:08:56.843Z","repository":{"id":174931323,"uuid":"652478987","full_name":"overtrue/php-opencc","owner":"overtrue","description":"中文简繁转换，支持词汇级别的转换、异体字转换和地区习惯用词转换（中国大陆、台湾、香港、日本新字体）。","archived":false,"fork":false,"pushed_at":"2025-08-12T03:02:26.000Z","size":1009,"stargazers_count":97,"open_issues_count":0,"forks_count":14,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-08-12T05:26:02.775Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/overtrue.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["overtrue"]}},"created_at":"2023-06-12T06:49:29.000Z","updated_at":"2025-08-12T03:02:29.000Z","dependencies_parsed_at":null,"dependency_job_id":"393037c8-4d16-4bfa-b8d3-244ac0ab5611","html_url":"https://github.com/overtrue/php-opencc","commit_stats":null,"previous_names":["overtrue/php-opencc"],"tags_count":5,"template":false,"template_full_name":"overtrue/php-package","purl":"pkg:github/overtrue/php-opencc","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/overtrue%2Fphp-opencc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/overtrue%2Fphp-opencc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/overtrue%2Fphp-opencc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/overtrue%2Fphp-opencc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/overtrue","download_url":"https://codeload.github.com/overtrue/php-opencc/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/overtrue%2Fphp-opencc/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270099983,"owners_count":24527042,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-12T02:00:09.011Z","response_time":80,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-28T20:40:26.769Z","updated_at":"2025-08-13T14:08:56.815Z","avatar_url":"https://github.com/overtrue.png","language":"PHP","funding_links":["https://github.com/sponsors/overtrue"],"categories":[],"sub_categories":[],"readme":"# PHP OpenCC\n\n中文简繁转换，支持词汇级别的转换、异体字转换和地区习惯用词转换（中国大陆、台湾、香港、日本新字体）。基于 [BYVoid/OpenCC](https://github.com/BYVoid/OpenCC) 数据实现。\n\n[![Build Status](https://github.com/overtrue/php-opencc/actions/workflows/test.yml/badge.svg)](https://github.com/overtrue/php-opencc/actions/workflows/test.yml)\n[![Latest Stable Version](https://poser.pugx.org/overtrue/php-opencc/v/stable)](https://packagist.org/packages/overtrue/php-opencc)\n[![Total Downloads](https://poser.pugx.org/overtrue/php-opencc/downloads)](https://packagist.org/packages/overtrue/php-opencc)\n[![License](https://poser.pugx.org/overtrue/php-opencc/license)](https://packagist.org/packages/overtrue/php-opencc)\n\n## 安装\n\n```shell\ncomposer require overtrue/php-opencc -vvv\n```\n\n## 使用\n\n```php\nuse Overtrue\\PHPOpenCC\\OpenCC;\n\necho OpenCC::convert('服务器', 'SIMPLIFIED_TO_TAIWAN_WITH_PHRASE');\n// output: 伺服器\n\n// 批量转换\nOpenCC::convert(['程序员', '服务器'], 'SIMPLIFIED_TO_TAIWAN_WITH_PHRASE');\n// output: ['程式設計師', '伺服器']\n```\n\n### 使用策略别名\n\n```php\nuse Overtrue\\PHPOpenCC\\OpenCC;\nuse Overtrue\\PHPOpenCC\\Strategy;\n\n// 以下方法等价：\n\n// 方法\necho OpenCC::s2tw('服务器');\necho OpenCC::simplifiedToTaiwan('服务器');\n\n// 字符串\necho OpenCC::convert('服务器', 's2tw');\necho OpenCC::convert('服务器', 'S2TW');\necho OpenCC::convert('服务器', 'SIMPLIFIED_TO_TAIWAN');\n\n// 常量\necho OpenCC::convert('服务器', Strategy::S2TW);\necho OpenCC::convert('服务器', Strategy::SIMPLIFIED_TO_TAIWAN);\n```\n\n### 流式/文件转换（可选）\n\n```php\nuse Overtrue\\PHPOpenCC\\StreamConverter;\n\n// 流到流\ntouch('out.txt');\n$in = fopen('input.txt', 'rb');\n$out = fopen('out.txt', 'wb');\nStreamConverter::convertStream($in, $out, 'S2T');\n\n// 文件到文件\nStreamConverter::convertFile('input.txt', 'out.txt', 'S2TWP');\n```\n\n\u003e 注意：按行转换不适用于跨行的词组替换场景。\n\n### 转换策略\n\n| 策略 （别名）                                   | 说明              |\n|-------------------------------------------|-----------------|\n| `SIMPLIFIED_TO_TRADITIONAL(S2T)`          | 简体到繁体           |\n| `SIMPLIFIED_TO_HONGKONG(S2HK)`            | 简体到香港繁体         |\n| `SIMPLIFIED_TO_JAPANESE(S2JP)`            | 简体到日文           |\n| `SIMPLIFIED_TO_TAIWAN(S2TW)`              | 简体到台湾正体         |\n| `SIMPLIFIED_TO_TAIWAN_WITH_PHRASE(2TWP)`  | 简体到台湾正体, 带词汇本地化 |\n| `HONGKONG_TO_TRADITIONAL(HK2T)`           | 香港繁体到正体         |\n| `HONGKONG_TO_SIMPLIFIED(HK2S)`            | 香港繁体到简体         |\n| `TAIWAN_TO_SIMPLIFIED(TW2S)`              | 台湾正体到简体         |\n| `TAIWAN_TO_TRADITIONAL(TW2T)`             | 台湾正体到繁体         |\n| `TAIWAN_TO_SIMPLIFIED_WITH_PHRASE(TW2SP)` | 台湾正体到简体, 带词汇本地化 |\n| `TRADITIONAL_TO_HONGKONG(T2HK)`           | 正体到香港繁体         |\n| `TRADITIONAL_TO_SIMPLIFIED(T2S)`          | 繁体到简体           |\n| `TRADITIONAL_TO_TAIWAN(T2TW)`             | 繁体到台湾正体         |\n| `TRADITIONAL_TO_JAPANESE(T2JP)`           | 繁体到日文           |\n| `JAPANESE_TO_TRADITIONAL(JP2T)`           | 日文到繁体           |\n| `JAPANESE_TO_SIMPLIFIED(JP2S)`            | 日文到简体           |\n\n### 在命令行使用\n\n```shell\nphp vendor/bin/opencc \"汉字\" s2tw\n```\n\n说明：\n\n```bash\n$ php vendor/bin/opencc --help\nDescription:\n  中文简繁转换，支持词汇级别的转换、异体字转换和地区习惯用词转换（中国大陆、台湾、香港、日本新字体）。\n\nUsage:\n  convert \u003cstring\u003e [\u003cstrategy\u003e]\n\nArguments:\n  string                待转换的字符串\n  strategy              转换策略 [default: \"SIMPLIFIED_TO_TRADITIONAL\"]\n```\n\n### 性能与部署建议\n\n- 强烈建议在生产环境开启并配置 Opcache，并将常用字典文件加入 `opcache.preload`，可显著降低首次调用延迟。\n  - 典型 php.ini 片段（示例）：\n    ```ini\n    opcache.enable=1\n    opcache.enable_cli=1\n    opcache.preload=/path/to/preload.php\n    ```\n  - `preload.php` 示例：\n    ```php\n    \u003c?php\n    // 预加载常用字典，路径请根据实际部署调整\n    require __DIR__.'/vendor/autoload.php';\n    foreach ([\n        'STPhrases','STCharacters','TWVariants','HKVariants','JPVariants',\n        'TSPhrases','TSCharacters','TWPhrases','TWVariantsRev','JPVariantsRev',\n    ] as $name) {\n        require __DIR__.'/vendor/overtrue/php-opencc/data/parsed/'.$name.'.php';\n    }\n    ```\n- 本库会对字典进行“合并+按键长降序排序”的预处理并缓存于进程内存；在常驻进程（如 FPM、Swoole、RoadRunner）下复用效果更佳。\n\n### 构建工具（可移植）\n\n- `bin/opencc build` 使用纯 PHP（`ZipArchive` + 递归拷贝）下载并解析 upstream 字典，无需系统级 `curl/unzip/cp` 命令。\n- 需要的 PHP 扩展：`ext-zip`、`ext-mbstring`。\n\n## :heart: 赞助我\n\n如果你喜欢我的项目并想支持它，[点击这里 :heart:](https://github.com/sponsors/overtrue)\n\n## 参与贡献\n\nYou can contribute in one of three ways:\n\n1. File bug reports using the [issue tracker](https://github.com/overtrue/php-opencc/issues).\n2. Answer questions or fix bugs on the [issue tracker](https://github.com/overtrue/php-opencc/issues).\n3. Contribute new features or update the wiki.\n\n_The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines. Any new code contributions must be accompanied by unit tests where applicable._\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fovertrue%2Fphp-opencc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fovertrue%2Fphp-opencc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fovertrue%2Fphp-opencc/lists"}