PHPackages                             jundayw/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. jundayw/pinyin

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

jundayw/pinyin
==============

Chinese characters to Pinyin.

v3.0.0(3y ago)02.5k↑33.3%MITPHPPHP &gt;=7.2.0

Since Jul 13Pushed 1y ago1 watchersCompare

[ Source](https://github.com/jundayw/pinyin)[ Packagist](https://packagist.org/packages/jundayw/pinyin)[ RSS](/packages/jundayw-pinyin/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (4)Used By (0)

PinYin
======

[](#pinyin)

Chinese Characters to Pinyin.

安装
--

[](#安装)

使用 Composer 安装:

```
composer require jundayw/pinyin
```

[![Latest Stable Version](https://camo.githubusercontent.com/d85dcb72ec56b90dbd1961b8bf1cd01340c4d24c83b40d037ab296bbe0dd8f57/68747470733a2f2f706f7365722e707567782e6f72672f6a756e646179772f70696e79696e2f76)](https://packagist.org/packages/jundayw/pinyin)[![Total Downloads](https://camo.githubusercontent.com/490b11496eda41c591bfbf2114c0cfa85bae0e0614d73ae3cedd9d7009af0c03/68747470733a2f2f706f7365722e707567782e6f72672f6a756e646179772f70696e79696e2f646f776e6c6f616473)](https://packagist.org/packages/jundayw/pinyin)[![Latest Unstable Version](https://camo.githubusercontent.com/917bac354f4dfeb5a88efeb47d743e41da3dd09c565aff4589b1de30b2486a4b/68747470733a2f2f706f7365722e707567782e6f72672f6a756e646179772f70696e79696e2f762f756e737461626c65)](https://packagist.org/packages/jundayw/pinyin)[![License](https://camo.githubusercontent.com/6f8327803376e2463ee87fc636102614abd498e5be8abfa56bd5823f83d98019/68747470733a2f2f706f7365722e707567782e6f72672f6a756e646179772f70696e79696e2f6c6963656e7365)](https://packagist.org/packages/jundayw/pinyin)[![PHP Version Require](https://camo.githubusercontent.com/b6f9c1a5cfc944504e3ba712d7ad980cf661cc5694793563143d8300964ad217/68747470733a2f2f706f7365722e707567782e6f72672f6a756e646179772f70696e79696e2f726571756972652f706870)](https://packagist.org/packages/jundayw/pinyin)

### 原生 `PHP` 中使用：

[](#原生-php-中使用)

```
use Jundayw\PinYin\PinYin;

$config = include("../config/pinyin.php");

$pinyin = new PinYin($config);

$pinyin->name('单某某');
```

### 框架 `Laravel` 中使用：

[](#框架-laravel-中使用)

```
use Jundayw\PinYin\Facades\Alphabet;
use Jundayw\PinYin\PinYin;

// 门面使用方法
Alphabet::name('单某某', PinYin::PINYIN_NAME);

// 依赖注入
public function test(PinYin $pinyin) {
    return $pinyin->name('单某某');
}
```

发布配置文件

```
php artisan vendor:publish --tag=pinyin-config
```

### 框架 `ThinkPHP` 中使用（ThinkPHP6）：

[](#框架-thinkphp-中使用thinkphp6)

```
use Jundayw\PinYin\Services\Alphabet;
use Jundayw\PinYin\PinYin;

// 门面使用方法
Alphabet::name('单某某', PinYin::PINYIN_NAME);

// 依赖注入
public function test(PinYin $pinyin) {
    return $pinyin->name('单某某');
}
```

发布配置文件

```
php think vendor:publish
```

可用选项：
-----

[](#可用选项)

选项描述`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`### 拼音数组

[](#拼音数组)

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

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

### 翻译姓名

[](#翻译姓名)

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

```
name(string $chinese, int $option = PinYin::PINYIN_NAME)
```

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

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

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

```
kebab(string $chinese, string $delimiter = null, int $option = PinYin::PINYIN_DEFAULT  | PinYin::PINYIN_KEEP_NUMBER | PinYin::PINYIN_KEEP_ENGLISH)
```

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

### 获取首字符字符串

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

```
abbr(string $chinese, string $delimiter = null, int $option = PinYin::PINYIN_DEFAULT | PinYin::PINYIN_NO_TONE)
```

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

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

### 翻译汉语短语

[](#翻译汉语短语)

```
phrase(string $chinese, string $delimiter = null, int $option = PinYin::PINYIN_DEFAULT)
```

```
$pinyin->phrase('新年快乐，2022 Happy New Year!');
// xin nian kuai le

// 数字、英文及标点符号不推荐使用 phrase 函数方法处理
$pinyin->phrase('新年快乐，2022 Happy New Year!', '-', PinYin::PINYIN_TONE | PinYin::PINYIN_KEEP_PUNCTUATION | PinYin::PINYIN_KEEP_ENGLISH | PinYin::PINYIN_KEEP_NUMBER);
// xīn-nián-kuài-lè,-2022-Happy-New-Year!
```

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

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

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

```
sentence(string $chinese, string $delimiter = null, int $option = PinYin::PINYIN_NO_TONE | PinYin::PINYIN_KEEP_PUNCTUATION | PinYin::PINYIN_KEEP_ENGLISH | PinYin::PINYIN_KEEP_NUMBER)
```

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

$pinyin->sentence('带着希望去旅行，比到达终点更美好！', null, PinYin::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!
```

配置文件格式
------

[](#配置文件格式)

> 使用 `tab` 制表符作为分隔符

```
[

    /*
    |--------------------------------------------------------------------------
    | DictLoader Driver
    |--------------------------------------------------------------------------
    |
    | Supported Drivers:
    | \Jundayw\PinYin\Support\FileDictLoader
    | \Jundayw\PinYin\Support\GeneratorFileDictLoader
    | \Jundayw\PinYin\Support\MemoryFileDictLoader
    |
    | Default:
    | \Jundayw\PinYin\Support\FileDictLoader
    |
    */

    'driver' => null,

    /*
    |--------------------------------------------------------------------------
    | 姓氏处理
    |--------------------------------------------------------------------------
    |
    */

    'surnames' => [
        '单' => '	shàn',
    ],

    /*
    |--------------------------------------------------------------------------
    | 单词处理
    |--------------------------------------------------------------------------
    | 注意：优先级高于短语处理
    |
    */

    'chars' => [
        '⺁' => '	fǎn',
    ],

    /*
    |--------------------------------------------------------------------------
    | 短语处理
    |--------------------------------------------------------------------------
    |
    */

    'words' => [
        '单田芳' => '	shàn	tián	fāng',
        '一路同行' => '	yí	lù	tóng	xíng',
    ],
];
```

可选字典驱动类型：

1、Jundayw\\PinYin\\Support\\MemoryFileDictLoader

- 内存型，适用于服务器内存空间较富余
- 优点：转换快
- 将所有字典预先载入内存

2、Jundayw\\PinYin\\Support\\FileDictLoader

- 小内存型（默认），适用于内存比较紧张的环境
- 优点：占用内存小，转换不如内存型快
- 将字典分片载入内存

3、Jundayw\\PinYin\\Support\\GeneratorFileDictLoader

- I/O型，适用于虚拟机，内存限制比较严格环境
- 优点：非常微小内存消耗
- 缺点：转换慢，不如内存型转换快
- 不载入内存
- 将字典使用文件流打开逐行遍历并运用 `php5.5` 生成器 `yield` 特性分配单行内存

鸣谢
--

[](#鸣谢)

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

> 项目源于 [overtrue/pinyin](https://github.com/overtrue/pinyin) ，因词库完善及补充不能满足业务时效性需求，此项目才应运而生。

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance32

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~3 days

Total

3

Last Release

1389d ago

Major Versions

v1.0.0 → v2.0.02022-07-19

v2.0.0 → v3.0.02022-07-20

### Community

Maintainers

![](https://www.gravatar.com/avatar/2da9b458375a1b7972b7c4d26a5bf8f3e48db305e8805da36f253956f33c5568?d=identicon)[jundayw](/maintainers/jundayw)

---

Top Contributors

[![jundayw](https://avatars.githubusercontent.com/u/16873970?v=4)](https://github.com/jundayw "jundayw (9 commits)")

---

Tags

laravelphppinyinthinkphpphplaravelPinyinthinkphp

### Embed Badge

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

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

###  Alternatives

[amranidev/laracombee

Recommendation system for laravel

11636.7k1](/packages/amranidev-laracombee)[salmanzafar/laravel-geocode

A Laravel Library to find Lat and Long of a given Specific Address

153.9k](/packages/salmanzafar-laravel-geocode)[wujunze/money-wrapper

MoneyPHP Wrapper

113.8k](/packages/wujunze-money-wrapper)

PHPackages © 2026

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