PHP code example of mucts / pinyin

1. Go to this page and download the library: Download mucts/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/ */

    

mucts / pinyin example snippets


use MuCTS\Pinyin\Pinyin;
use MuCTS\Pinyin\Loaders\MemoryFile;
use MuCTS\Pinyin\Loaders\GeneratorFile;

// 小内存型
$pinyin = new Pinyin(); // 默认
// 内存型
// $pinyin = new Pinyin(MemoryFile::class);
// I/O型
// $pinyin = new Pinyin(GeneratorFile::class);

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

$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!

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