PHP code example of jundayw / pinyin

1. Go to this page and download the library: Download jundayw/pinyin library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jundayw / pinyin example snippets


use Jundayw\PinYin\PinYin;

$config = yin->name('单某某');

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

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

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

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

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

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

$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"]

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!

[

    /*
    |--------------------------------------------------------------------------
    | 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',
    ],
];
shell
php artisan vendor:publish --tag=pinyin-config
shell
php think vendor:publish