PHPackages                             gerpayt/pinyin - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. gerpayt/pinyin

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

gerpayt/pinyin
==============

Chinese to pinyin translator.

4.0.0(7y ago)045MITPHPPHP &gt;=5.3

Since Jul 16Pushed 6y ago1 watchersCompare

[ Source](https://github.com/gerpayt/pinyin)[ Packagist](https://packagist.org/packages/gerpayt/pinyin)[ Docs](https://github.com/overtrue/pinyin)[ RSS](/packages/gerpayt-pinyin/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (1)Versions (36)Used By (0)

Pinyin
======

[](#pinyin)

[![Build Status](https://camo.githubusercontent.com/e868b4f9dde2482f5d56729f487dceffc02487d44bc58cd221bdc2394da06486/68747470733a2f2f7472617669732d63692e6f72672f6f766572747275652f70696e79696e2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/overtrue/pinyin)[![Latest Stable Version](https://camo.githubusercontent.com/10cbf8c470851ad0bebd058635ad6a781608dfc585994e7837e6578735bf8bb0/68747470733a2f2f706f7365722e707567782e6f72672f6f766572747275652f70696e79696e2f762f737461626c652e737667)](https://packagist.org/packages/overtrue/pinyin) [![Total Downloads](https://camo.githubusercontent.com/7a1ba91c5227ad10249bc6b1c7ca4d01a91e4151b8c6148e02594049132fe200/68747470733a2f2f706f7365722e707567782e6f72672f6f766572747275652f70696e79696e2f646f776e6c6f6164732e737667)](https://packagist.org/packages/overtrue/pinyin) [![Latest Unstable Version](https://camo.githubusercontent.com/33da35dba8b7f80211bb8dec99ae1d4feec1dc089a0c2f653dcad00c58f49c1a/68747470733a2f2f706f7365722e707567782e6f72672f6f766572747275652f70696e79696e2f762f756e737461626c652e737667)](https://packagist.org/packages/overtrue/pinyin) [![License](https://camo.githubusercontent.com/b66704519178e7ac6e1749812b75fcb59d56d65e2510f866b968d8a27ce23846/68747470733a2f2f706f7365722e707567782e6f72672f6f766572747275652f70696e79696e2f6c6963656e73652e737667)](https://packagist.org/packages/overtrue/pinyin)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/f787f804d17068ed0c89625b419266d63ddad6ef77aefa5391e0719fa80e702b/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6f766572747275652f70696e79696e2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/overtrue/pinyin/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/100caeca7a948ca9153444f23f978c46d431185e4350b0e93674ffddcaf91c39/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6f766572747275652f70696e79696e2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/overtrue/pinyin/?branch=master)[![FOSSA Status](https://camo.githubusercontent.com/6ba8e0e9afa6a18786ac7b231f6dbc2f0b732301c874e1cd84c68407a3a6619d/68747470733a2f2f6170702e666f7373612e696f2f6170692f70726f6a656374732f6769742532426769746875622e636f6d2532466f7665727472756525324670696e79696e2e7376673f747970653d736869656c64)](https://app.fossa.io/projects/git%2Bgithub.com%2Fovertrue%2Fpinyin?ref=badge_shield)

🇨🇳 基于 [CC-CEDICT](http://cc-cedict.org/wiki/) 词典的中文转拼音工具，更准确的支持多音字的汉字转拼音解决方案。

安装
--

[](#安装)

使用 Composer 安装:

```
$ composer require "overtrue/pinyin:~4.0"

```

使用
--

[](#使用)

可选转换方案：

- 内存型，适用于服务器内存空间较富余，优点：转换快
- 小内存型(默认)，适用于内存比较紧张的环境，优点：占用内存小，转换不如内存型快
- I/O型，适用于虚拟机，内存限制比较严格环境。优点：非常微小内存消耗。缺点：转换慢，不如内存型转换快,php &gt;= 5.5

可用选项：
-----

[](#可用选项)

选项描述`PINYIN_TONE`UNICODE 式音调：`měi hǎo``PINYIN_ASCII_TONE`带数字式音调： `mei3 hao3``PINYIN_NO_TONE`无音调：`mei hao``PINYIN_KEEP_NUMBER`保留数字`PINYIN_KEEP_ENGLISH`保留英文`PINYIN_KEEP_PUNCTUATION`保留标点`PINYIN_UMLAUT_V`使用 `v` 代替 `yu`, 例如：吕 `lyu` 将会转为 `lv`### 拼音数组

[](#拼音数组)

```
use Overtrue\Pinyin\Pinyin;

// 小内存型
$pinyin = new Pinyin(); // 默认
// 内存型
// $pinyin = new Pinyin('Overtrue\Pinyin\MemoryFileDictLoader');
// I/O型
// $pinyin = new Pinyin('Overtrue\Pinyin\GeneratorFileDictLoader');

$pinyin->convert('带着希望去旅行，比到达终点更美好');
// ["dai", "zhe", "xi", "wang", "qu", "lyu", "xing", "bi", "dao", "da", "zhong", "dian", "geng", "mei", "hao"]

$pinyin->convert('带着希望去旅行，比到达终点更美好', PINYIN_TONE);
// ["dài","zhe","xī","wàng","qù","lǚ","xíng","bǐ","dào","dá","zhōng","diǎn","gèng","měi","hǎo"]

$pinyin->convert('带着希望去旅行，比到达终点更美好', PINYIN_ASCII_TONE);
//["dai4","zhe","xi1","wang4","qu4","lyu3","xing2","bi3","dao4","da2","zhong1","dian3","geng4","mei3","hao3"]
```

- 小内存型: 将字典分片载入内存
- 内存型: 将所有字典预先载入内存
- I/O型: 不载入内存，将字典使用文件流打开逐行遍历并运用php5.5生成器(yield)特性分配单行内存

### 生成用于链接的拼音字符串

[](#生成用于链接的拼音字符串)

```
$pinyin->permalink('带着希望去旅行'); // dai-zhe-xi-wang-qu-lyu-xing
$pinyin->permalink('带着希望去旅行', '.'); // dai.zhe.xi.wang.qu.lyu.xing
```

### 获取首字符字符串

[](#获取首字符字符串)

```
$pinyin->abbr('带着希望去旅行'); // dzxwqlx
$pinyin->abbr('带着希望去旅行', '-'); // d-z-x-w-q-l-x

$pinyin->abbr('你好2018！', PINYIN_KEEP_NUMBER); // nh2018
$pinyin->abbr('Happy New Year! 2018！', PINYIN_KEEP_ENGLISH); // HNY2018
```

### 翻译整段文字为拼音

[](#翻译整段文字为拼音)

将会保留中文字符：`，。 ！ ？ ： “ ” ‘ ’` 并替换为对应的英文符号。

```
$pinyin->sentence('带着希望去旅行，比到达终点更美好！');
// dai zhe xi wang qu lyu xing, bi dao da zhong dian geng mei hao!

$pinyin->sentence('带着希望去旅行，比到达终点更美好！', PINYIN_TONE);
// dài zhe xī wàng qù lǚ xíng, bǐ dào dá zhōng diǎn gèng měi hǎo!
```

### 翻译姓名

[](#翻译姓名)

姓名的姓的读音有些与普通字不一样，比如 ‘单’ 常见的音为 `dan`，而作为姓的时候读 `shan`。

```
$pinyin->name('单某某'); // ['shan', 'mou', 'mou']
$pinyin->name('单某某', PINYIN_TONE); // ["shàn","mǒu","mǒu"]
```

更多使用请参考 [测试用例](https://github.com/overtrue/pinyin/blob/master/tests/AbstractDictLoaderTestCase.php)。

在 Laravel 中使用
-------------

[](#在-laravel-中使用)

独立的包在这里：[overtrue/laravel-pinyin](https://github.com/overtrue/laravel-pinyin)

Contribution
------------

[](#contribution)

欢迎提意见及完善补充词库 [`overtrue/pinyin-dictionary-maker`](https://github.com/overtrue/pinyin-dictionary-maker/tree/master/patches) 💋

参考
--

[](#参考)

- [详细参考资料](https://github.com/overtrue/pinyin-resources)

License
=======

[](#license)

[![FOSSA Status](https://camo.githubusercontent.com/7693b33a63b5f0d430c9814a3008d72956a32f3e92fd58f53cf4db9346524426/68747470733a2f2f6170702e666f7373612e696f2f6170692f70726f6a656374732f6769742532426769746875622e636f6d2532466f7665727472756525324670696e79696e2e7376673f747970653d6c61726765)](https://app.fossa.io/projects/git%2Bgithub.com%2Fovertrue%2Fpinyin?ref=badge_large)

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 91.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~45 days

Recently: every ~207 days

Total

35

Last Release

2793d ago

Major Versions

1.4 → 2.02014-10-08

v2.x-dev → 3.0.02016-04-25

3.0.6 → 4.0.02018-09-20

PHP version history (2 changes)3.0.1PHP &gt;=5.4.0

3.0.2PHP &gt;=5.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/006518298f7befe8f7154019629953d8cf0416935f531d865bca279904a63888?d=identicon)[gerpayt](/maintainers/gerpayt)

---

Top Contributors

[![overtrue](https://avatars.githubusercontent.com/u/1472352?v=4)](https://github.com/overtrue "overtrue (276 commits)")[![gerpayt](https://avatars.githubusercontent.com/u/3348817?v=4)](https://github.com/gerpayt "gerpayt (5 commits)")[![xjchengo](https://avatars.githubusercontent.com/u/4819996?v=4)](https://github.com/xjchengo "xjchengo (4 commits)")[![medz](https://avatars.githubusercontent.com/u/5564821?v=4)](https://github.com/medz "medz (3 commits)")[![drl3fty](https://avatars.githubusercontent.com/u/39155553?v=4)](https://github.com/drl3fty "drl3fty (1 commits)")[![fossabot](https://avatars.githubusercontent.com/u/29791463?v=4)](https://github.com/fossabot "fossabot (1 commits)")[![garveen](https://avatars.githubusercontent.com/u/3214266?v=4)](https://github.com/garveen "garveen (1 commits)")[![liuxd](https://avatars.githubusercontent.com/u/678977?v=4)](https://github.com/liuxd "liuxd (1 commits)")[![abrahamgreyson](https://avatars.githubusercontent.com/u/1341308?v=4)](https://github.com/abrahamgreyson "abrahamgreyson (1 commits)")[![N0ah](https://avatars.githubusercontent.com/u/1782836?v=4)](https://github.com/N0ah "N0ah (1 commits)")[![totoleo](https://avatars.githubusercontent.com/u/4956722?v=4)](https://github.com/totoleo "totoleo (1 commits)")[![wangfenjin](https://avatars.githubusercontent.com/u/4160646?v=4)](https://github.com/wangfenjin "wangfenjin (1 commits)")[![wfyhehe](https://avatars.githubusercontent.com/u/25923719?v=4)](https://github.com/wfyhehe "wfyhehe (1 commits)")[![wiseker](https://avatars.githubusercontent.com/u/5337474?v=4)](https://github.com/wiseker "wiseker (1 commits)")[![luokuncool](https://avatars.githubusercontent.com/u/6022635?v=4)](https://github.com/luokuncool "luokuncool (1 commits)")[![aizuyan](https://avatars.githubusercontent.com/u/6612863?v=4)](https://github.com/aizuyan "aizuyan (1 commits)")[![Aufree](https://avatars.githubusercontent.com/u/5310542?v=4)](https://github.com/Aufree "Aufree (1 commits)")[![domainname](https://avatars.githubusercontent.com/u/1830237?v=4)](https://github.com/domainname "domainname (1 commits)")

---

Tags

PinyinChinesecn2pinyin

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/gerpayt-pinyin/health.svg)

```
[![Health](https://phpackages.com/badges/gerpayt-pinyin/health.svg)](https://phpackages.com/packages/gerpayt-pinyin)
```

###  Alternatives

[overtrue/pinyin

Chinese to pinyin translator.

4.5k6.5M72](/packages/overtrue-pinyin)[zhuzhichao/ip-location-zh

get the ip's location info with Chinese

617212.5k11](/packages/zhuzhichao-ip-location-zh)[overtrue/chinese-calendar

中国农历转换与查询工具

557103.7k4](/packages/overtrue-chinese-calendar)[zhuzhichao/bank-card-info

Get the bank card info

45464.8k](/packages/zhuzhichao-bank-card-info)[douyasi/identity-card

Chinese Identity Card package

17851.4k3](/packages/douyasi-identity-card)[thl/pinyin

Romanization tool for Traditional Chinese

2519.1k](/packages/thl-pinyin)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
