{"id":13700153,"url":"https://github.com/bullsoft/php-pinyin","last_synced_at":"2025-05-04T18:34:32.707Z","repository":{"id":8146613,"uuid":"9566092","full_name":"bullsoft/php-pinyin","owner":"bullsoft","description":"A PHP extension converting Chinese characters to Pinyin","archived":false,"fork":false,"pushed_at":"2017-06-21T01:48:57.000Z","size":7868,"stargazers_count":86,"open_issues_count":0,"forks_count":24,"subscribers_count":13,"default_branch":"master","last_synced_at":"2024-11-13T06:33:11.307Z","etag":null,"topics":["baidu","cpp11","glibcxx-use-cxx11-abi","php-cpp","php-pinyin","php7","pinyin"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bullsoft.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-04-20T15:25:28.000Z","updated_at":"2024-03-05T11:35:54.000Z","dependencies_parsed_at":"2022-09-14T13:30:53.584Z","dependency_job_id":null,"html_url":"https://github.com/bullsoft/php-pinyin","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bullsoft%2Fphp-pinyin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bullsoft%2Fphp-pinyin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bullsoft%2Fphp-pinyin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bullsoft%2Fphp-pinyin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bullsoft","download_url":"https://codeload.github.com/bullsoft/php-pinyin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252383043,"owners_count":21739263,"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","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":["baidu","cpp11","glibcxx-use-cxx11-abi","php-cpp","php-pinyin","php7","pinyin"],"created_at":"2024-08-02T20:00:49.092Z","updated_at":"2025-05-04T18:34:27.654Z","avatar_url":"https://github.com/bullsoft.png","language":"C++","funding_links":[],"categories":["字符串处理"],"sub_categories":[],"readme":"php-pinyin\n==========\n\nA PHP extension converting Chinese characters to Pinyin.\n\n一个来自百度的汉字转拼音PHP扩展，其他的汉字转拼音方案存在两个问题：\n\n1. 可转的汉字数有限，几千个左右\n2. 不能解决多音字问题\n\nInstallation\n============\n\nCurrently you have two ways to use php-pinyin. One depends on PHP-CPP, while another one is plain php extenstion which works with php 7.x. (For php 5.x support, please checkout the branch `legacy`)\n\n## Method with PHP-CPP\n\nMain improvements:\n  - Depend PHP-CPP, an awesome library which wrapper Zend Engine with friendly api\n  - Support PHP 7\n  - This time we support `UTF-8` and `GBK` encoding\n  - Add ini_setting (`pinyin.dict_path` and `pinyin.dict_tone`), you shoud not loadDict yourself.\n\n### Install\n\n1. Install [PHP-CPP](https://github.com/CopernicaMarketingSoftware/PHP-CPP) or its [LEGACY Version](https://github.com/CopernicaMarketingSoftware/PHP-CPP-LEGACY). Before that, you need to change the Makefile,,, because PHP-CPP was written with C++11, but libpinyin was written with C++98,,, So you should build PHP-CPP with `-D_GLIBCXX_USE_CXX11_ABI=0` option, which means \"Do not use Cxx11's Application Binary Interface\"\n2. cd /path/to/php-pinyin/cpp-ext\n3. make\n4. make install\n\n## Method without PHP-CPP\n\nThis is upgraded from old php-pinyin for php 5.x.\n\n### Install\n\n1. cd /path/to/php-pinyin/ext\n2. /path/to/php/bin/phpize\n3. ./configure --with-php-config=/path/to/php/bin/php-config --with-baidu-pinyin=/path/to/pinyin\n4. make\n5. make install\n\nHere `/path/to/pinyin` is the directory where you copied `libpinyin` to.\n\nUsage\n-----\n\n```php\n$obj  = new Pinyin();\n\n// UTF-8\nvar_dump($obj-\u003econvert(\"重庆重量\"));\nvar_dump($obj-\u003emultiConvert(array(\"重庆南京市长江大桥财务会议会计\")));\n\n// GBK\nvar_dump($obj-\u003emultiConvert(array(iconv(\"UTF-8\", \"GBK\", \"重庆\"), iconv(\"UTF-8\", \"GBK\", \"重量\"))));\n```\n\nResults will be:\n```php\nstring(22) \"chong'qing'zhong'liang\"\narray(1) {\n  [0] =\u003e\n  string(65) \"chong'qing'nan'jing'shi'chang'jiang'da'qiao'cai'wu'hui'yi'kuai'ji\"\n}\narray(2) {\n  [0] =\u003e\n  string(10) \"chong'qing\"\n  [1] =\u003e\n  string(11) \"zhong'liang\"\n}\narray(1) {\n  [0] =\u003e\n  string(29) \"zhong'hua'ren'min'gong'he'guo\"\n}\n```\n\nIf you want to get the Abbr. of the whole pinyin-string, you can simply do this:\n\n```php\necho preg_replace(\"/\\'([a-zA-Z])[0-9a-zA-Z]*/e\", \"strtoupper('$1')\", \"'\".$py_string);\n```\n\nThis lib only support Chinese characters and english letters, or else it will return `false`. So you can write a `safeConvert` function to avoid this.\n\n```php\n$p = new Pinyin();\nfunction safeConvert($word, $pyOnly = true) {\n    global $p;\n    // UTF-8 regex for Chinese\n    $result = preg_match_all(\"/([\\x{4e00}-\\x{9fa5}]+)/iu\", $word, $matches);\n    if(!$result) {\n        throw new \\Exception(\"No Chinese characters in word\");\n    }\n\n    $pys = $p-\u003emultiConvert($matches[1]);\n    if($pyOnly == true) {\n        return implode(\"'\", $pys);\n    } else {\n        return str_replace($matches[1], $pys, $word);\n    }\n}\n```\n\nIf you want to customize dict-files yourself and then convert them to binary-format again, do it like this:\n```php\n$result = $obj-\u003egenerateDict(\"/home/work/local/pinyin/dict/dict.txt\", \"/home/work/tmp/dict.dat\");\n\nif($result) echo \"Generate complete\";\n```\n\nFeedback\n--------\n\nIssues and contributions are welcome.\n\nThank you!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbullsoft%2Fphp-pinyin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbullsoft%2Fphp-pinyin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbullsoft%2Fphp-pinyin/lists"}