• Stars
    star
    4,296
  • Rank 10,034 (Top 0.2 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 10 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🇨🇳 基于词库的中文转拼音优质解决方案

Pinyin

Test Latest Stable Version Total Downloads Latest Unstable Version License

🇨🇳 基于 mozillazg/pinyin-data 词典的中文转拼音工具,更准确的支持多音字的汉字转拼音解决方案。

喜欢我的项目?点击这里支持我

安装

使用 Composer 安装:

composer require overtrue/pinyin:^5.0

使用

拼音风格

除了获取首字母的方法外,所有方法都支持第二个参数,用于指定拼音的格式,可选值为:

  • symbol (默认)声调符号,例如 pīn yīn
  • none 不输出拼音,例如 pin yin
  • number 末尾数字模式的拼音,例如 pin1 yin1

返回值

除了 permalink 返回字符串外,其它方法都返回集合类型 Overtrue\Pinyin\Collection

use Overtrue\Pinyin\Pinyin;

$pinyin = Pinyin::sentence('你好,世界');

你可以通过以下方式访问集合内容:

echo $pinyin; // nǐ hǎo shì jiè

// 直接将对象转成字符串
$string = (string) $pinyin; // nǐ hǎo shì jiè

$pinyin->toArray(); // ['nǐ', 'hǎo', 'shì', 'jiè']

// 直接使用索引访问
$pinyin[0]; // 'nǐ'

// 使用函数遍历
$pinyin->map('ucfirst'); // ['Nǐ', 'Hǎo', 'Shì', 'Jiè']

// 拼接为字符串
$pinyin->join(' '); // 'nǐ hǎo shì jiè'
$pinyin->join('-'); // 'nǐ-hǎo-shì-jiè'

// 转成 json
$pinyin->toJson(); // '["nǐ","hǎo","shì","jiè"]'
json_encode($pinyin); // '["nǐ","hǎo","shì","jiè"]'

文字段落转拼音

use Overtrue\Pinyin\Pinyin;

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

// 去除声调
echo Pinyin::sentence('带着希望去旅行,比到达终点更美好', 'none');
// dai zhe xi wang qu lv xing , bi dao da zhong dian geng mei hao

// 保留所有非汉字字符
echo Pinyin::fullSentence('ル是片假名,π是希腊字母', 'none');
// ル shi pian jia ming ,π shi xi la zi mu

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

通常用于文章链接等,可以使用 permalink 方法获取拼音字符串:

echo Pinyin::permalink('带着希望去旅行'); // dai-zhe-xi-wang-qu-lyu-xing
echo Pinyin::permalink('带着希望去旅行', '.'); // dai.zhe.xi.wang.qu.lyu.xing

获取首字符字符串

通常用于创建搜索用的索引,可以使用 abbr 方法转换:

Pinyin::abbr('带着希望去旅行'); // ['d', 'z', 'x', 'w', 'q', 'l', 'x']
echo Pinyin::abbr('带着希望去旅行')->join('-'); // d-z-x-w-q-l-x
echo Pinyin::abbr('你好2018!')->join(''); // nh2018
echo Pinyin::abbr('Happy New Year! 2018!')->join(''); // HNY2018

姓名首字母

将首字作为姓氏转换,其余作为普通词语转换:

Pinyin::nameAbbr('欧阳'); // ['o', 'y']
echo Pinyin::nameAbbr('单单单')->join('-'); // s-d-d

姓名转换

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

Pinyin::name('单某某'); // ['shàn', 'mǒu', 'mǒu']
Pinyin::name('单某某', 'none'); // ['shan', 'mou', 'mou']
Pinyin::name('单某某', 'none')->join('-'); // shan-mou-mou

护照姓名转换

根据国家规定 关于中国护照旅行证上姓名拼音 ü(吕、律、闾、绿、女等)统一拼写为 YU 的提醒 的规则,将 ü 转换为 yu

Pinyin::passportName('吕小布'); // ['lyu', 'xiao', 'bu']
Pinyin::passportName('女小花'); // ['nyu', 'xiao', 'hua']
Pinyin::passportName('律师'); // ['lyu', 'shi']

多音字

多音字的返回值为关联数组的集合,默认返回去重后的所有读音:

$pinyin = Pinyin::polyphones('重庆');

$pinyin['重']; // ["zhòng", "chóng", "tóng"]
$pinyin['庆']; // ["qìng"]

$pinyin->toArray();
// [
//     "重": ["zhòng", "chóng", "tóng"],
//     "庆": ["qìng"]
// ]

如果不想要去重,可以数组形式返回:

$pinyin = Pinyin::polyphones('重庆重庆', Converter::TONE_STYLE_SYMBOL, true);

// or 
$pinyin = Pinyin::polyphonesAsArray('重庆重庆', Converter::TONE_STYLE_SYMBOL);

$pinyin->toArray();
// [
//     ["重" => ["zhòng", "chóng", "tóng"]],
//     ["庆" => ["qìng"]],
//     ["重" => ["zhòng", "chóng", "tóng"]],
//     ["庆" => ["qìng"]]
// ]

单字转拼音

和多音字类似,单字的返回值为字符串,多音字将根据该字字频调整得到常用音:

$pinyin = Pinyin::chars('重庆');

echo $pinyin['重']; // "zhòng"
echo $pinyin['庆']; // "qìng"

$pinyin->toArray();
// [
//     "重": "zhòng",
//     "庆": "qìng"
// ]

Warning

当单字处理时由于多音字来自词频表中取得常用音,所以在词语环境下可能出现不正确的情况,建议使用多音字处理。

更多使用请参考 测试用例

v/yu/ü 的问题

根据国家语言文字工作委员会的规定,lvlyu 都是正确的,但是 lv 是最常用的,所以默认使用 lv,如果你需要使用其他的,可以在初始化时传入:

echo Pinyin::sentence('旅行');
// lǚ xíng

echo Pinyin::sentence('旅行', 'none');
// lv xing

echo Pinyin::yuToYu()->sentence('旅行', 'none');
// lyu xing

echo Pinyin::yuToU()->sentence('旅行', 'none');
// lu xing

echo Pinyin::yuToV()->sentence('旅行', 'none');
// lv xing

Warning

仅在拼音风格为非 none 模式下有效。

命令行工具

你可以使用命令行来实现拼音的转换:

php ./bin/pinyin 带着希望去旅行 --method=sentence --tone-style=symbol
# dài zhe xī wàng qù lǚ xíng

更多使用方法,可以查看帮助文档:

php ./bin/pinyin --help

# Usage:
#     ./pinyin [chinese] [method] [options]
# Options:
#     -j, --json               输出 JSON 格式.
#     -c, --compact            不格式化输出 JSON.
#     -m, --method=[method]    转换方式,可选:sentence/sentenceFull/permalink/abbr/nameAbbr/name/passportName/phrase/polyphones/chars.
#     --no-tone                不使用音调.
#     --tone-style=[style]     音调风格,可选值:symbol/none/number, default: none.
#     -h, --help               显示帮助.

在 Laravel 中使用

独立的包在这里:overtrue/laravel-pinyin

中文简繁转换

如何你有这个需求,也可以了解我的另一个包:overtrue/php-opencc

Contribution

欢迎提意见及完善补充词库:

参考

❤️ Sponsor me

Sponsor me

如果你喜欢我的项目并想支持它,点击这里 ❤️

Project supported by JetBrains

Many thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.

PHP 扩展包开发

想知道如何从零开始构建 PHP 扩展包?

请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— 《PHP 扩展包实战教程 - 从入门到发布》

License

MIT

More Repositories

1

share.js

一键分享到微博、QQ空间、QQ好友、微信、腾讯微博、豆瓣、Facebook、Twitter、Linkedin、Google+、点点等
JavaScript
3,691
star
2

easy-sms

📲 一款满足你的多种发送需求的短信发送组件
PHP
3,127
star
3

laravel-wechat

微信 SDK for Laravel, 基于 overtrue/wechat
PHP
2,865
star
4

socialite

Socialite is an OAuth2 Authentication tool. It is inspired by laravel/socialite, you can easily use it without Laravel.
PHP
1,294
star
5

laravel-lang

🌏 75 languages support for Laravel application.
PHP
1,271
star
6

laravel-follow

❤️ This package helps you to add user based follow system to your model.
PHP
1,183
star
7

api.yike.io

一刻社区后端 API 源码
PHP
1,021
star
8

phplint

🐛 A tool that can speed up linting of php files by running several lint processes at once.
PHP
981
star
9

yike.io

一刻社区前端源码
Vue
658
star
10

laravel-pinyin

🇨🇳 Chinese to Pinyin translator for Laravel 5 / Lumen
PHP
529
star
11

chinese-calendar

📅 中国农历(阴历)与阳历(公历)转换与查询工具
PHP
521
star
12

laravel-versionable

⏱️Make Laravel model versionable
PHP
510
star
13

vue-avatar-cropper

👧 A simple and elegant avatar cropping and upload plugin.
JavaScript
508
star
14

laravel-filesystem-qiniu

A Qiniu Storage filesystem for Laravel
PHP
471
star
15

laravel-like

👍 User-like features for Laravel Application.
PHP
467
star
16

laravel-favorite

❤️ User favorite feature for Laravel Application.
PHP
422
star
17

laravel-query-logger

📝 A dev tool to log all queries for laravel application.
PHP
408
star
18

laravel-shopping-cart

🛒 Shopping cart for Laravel Application.
PHP
392
star
19

laravel-ueditor

UEditor integration for Laravel.
JavaScript
390
star
20

laravel-socialite

:octocat: Social OAuth Authentication for Laravel 5. drivers: facebook, github, google, linkedin, weibo, qq, wechat and douban
PHP
339
star
21

latest-laravel

【不再更新】这些东西本不应该存在。
Shell
282
star
22

weibo-dogs-killer

一段屏蔽 HWB(微博监督员) 的 js 小脚本
275
star
23

wisteria

Beautiful document tool for your project.
Blade
255
star
24

flysystem-qiniu

💾 Flysystem adapter for the Qiniu storage.
PHP
221
star
25

package-builder

📦 A composer package builder.
PHP
199
star
26

weather

🌈 基于高德开放平台接口的 PHP 天气信息组件。
PHP
173
star
27

laravel-subscribe

📧 User Subscribe/Unsubscribe features for Laravel Application.
PHP
170
star
28

yike

PHP
164
star
29

laravel-emoji

😄 This package assist you in getting started with emoji easily.
PHP
151
star
30

vscode-miniapp-helper

微信小程序开发助手 for VSCode
JavaScript
150
star
31

laravel-skeleton

Laravel Application skeleton for me.
PHP
142
star
32

yaf-skeleton

The Yaf testable skeleton and composer supported.
PHP
141
star
33

laravel-uploader

🌴 An upload component for Laravel.
PHP
135
star
34

http

🌵 A simple http client wrapper.
PHP
126
star
35

writor

基于 Laravel 4 开发的博客系统
JavaScript
125
star
36

pinyin-resources

汉字拼音相关参考资料
123
star
37

laravel-vote

⬆️ ⬇️ User vote system for Laravel Application.
PHP
108
star
38

validation

Laravel Validation 简化无依赖版
PHP
100
star
39

bootstrap-theme-slim

纤细风格的bootstrap主题
HTML
92
star
40

city.js

城市选择控件
92
star
41

laravel-payment

Omnipay ServiceProvider for Laravel.
PHP
91
star
42

laravel-passport-cache-token

Make laravel/passport token cacheable.
PHP
89
star
43

laravel-filesystem-cos

Tencent Cloud COS storage for Laravel based on overtrue/flysystem-cos.
PHP
89
star
44

php-opencc

中文简繁转换,支持词汇级别的转换、异体字转换和地区习惯用词转换(中国大陆、台湾、香港、日本新字体)。
PHP
75
star
45

blog

安正超博客
Less
74
star
46

flysystem-cos

💾 Flysystem adapter for the Qcloud COS storage.
PHP
74
star
47

rester

基于 Slim+Eloquent 的 RESTful API 框架
PHP
71
star
48

laravel-mail-aliyun

📧 Aliyun DrirectMail Transport for Laravel Application.
PHP
71
star
49

laravel-youzan

【停止维护】Youzan wrapper for Laravel
PHP
59
star
50

websocket

A PHP implementation of WebSocket.
PHP
56
star
51

laravel-options

Global options module for Laravel application.
PHP
51
star
52

laravel.xyz

Source code of https://laravel.xyz
Vue
50
star
53

json-viewer

A tool for make JSON view in browser.
JavaScript
49
star
54

building-with-tailwindcss

HTML
45
star
55

php-package

A PHP package template repository.
45
star
56

laravel-qcloud-content-audit

腾讯云内容安全(文字图片内容审核)服务
PHP
44
star
57

phpmd-rulesets

PHP_MD 规则
42
star
58

sketch-data-cn

为 Sketch 准备的模拟数据中文版,包含:中文姓名,手机号,省份,城市,地区,公司名,银行名,星期几,详情地址,邮编,邮箱,颜色,广告词等。
41
star
59

qcloud-cos-client

Tencent COS Client
PHP
40
star
60

validator.js

一个类似laravel的js验证模块.
JavaScript
39
star
61

translator

PHP多语言支持工具
PHP
38
star
62

laravel-payable

Payment system for Laravel.
PHP
36
star
63

laravel-package

Laravel package template
PHP
35
star
64

laravel-revaluation

Laravel 5 model revaluation helper.
PHP
31
star
65

laravel-easy-sms

overtrue/easy-sms service provider for Laravel.
PHP
26
star
66

laravel-saml

SAML toolkit for Laravel based on OneLogin's SAML PHP Toolkit.
PHP
21
star
67

sendcloud

SendCloud Mail SDK
PHP
20
star
68

laravel-sendcloud

SendCloud Mail SDK for Laravel.
PHP
20
star
69

latest-lumen

【不再更新】这些东西本不应该存在。
Shell
19
star
70

laravel-stateless-session

A lightweight middleware to make api routing session capable.
PHP
18
star
71

laravel-single-session

A plugin provide single session authentication for Laravel 5.
PHP
17
star
72

php-multi-process-runner

基于PHP拓展PCNTL的多进程执行工具
PHP
17
star
73

laravel-summernote

Summernote editor integration for Laravel 5.
JavaScript
16
star
74

laravel-passport-cache-client

Make laravel/passport client cacheable.
PHP
16
star
75

laravel-qcloud-federation-token

QCloud COS FederationToken generator for Laravel.
PHP
16
star
76

cuttle

📃 A multi-module log wrapper.
PHP
15
star
77

stuq-laravel-course

StuQ小班课|Laravel实战经验分享及PHP后端编程思想
PHP
13
star
78

laravel.so

Source Code of http://laravel.so (old)
PHP
13
star
79

double-array-trie

PHP
12
star
80

overtrue

https://overtrue.me
12
star
81

bash-color

Generate command line colorized text.
PHP
12
star
82

pinyin-dictionary-maker

📇 Dictionary make of overtrue/pinyin.
PHP
12
star
83

relocator.js

relocate images to fit image box
JavaScript
11
star
84

laravel-open-telemetry

This package provides a simple way to add OpenTelemetry to your Laravel application.
PHP
11
star
85

youzan

【不再维护】Youzan SDK.
PHP
10
star
86

wisteria-skeleton

The skeleton of Wisteria.
PHP
10
star
87

wp-cn-excerpt

wordpress中文摘要插件
PHP
10
star
88

laravel-custom-log

📃 Make Laravel Log great again.
PHP
9
star
89

laravel-qcloud-captcha

QCloud Captcha service validator for Laravel.
PHP
7
star
90

vue-avatar-cropper-demo

Created with CodeSandbox
Vue
7
star
91

string2pinyin.sinaapp.com

PHP
3
star
92

nuxt-dashboard

CSS
3
star
93

wp-auto-top

WordPress 返回顶部插件
PHP
2
star
94

redius.xyz

wip
2
star
95

overtrue.github.io

HTML
2
star
96

username-blacklist

PHP
2
star
97

pinyin.rs

2
star
98

easywechat-theme

Vue
2
star
99

dex-swap-test

JavaScript
1
star
100

phplint-test

PHP
1
star