PHP code example of linfly / pinyin

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

    

linfly / pinyin example snippets


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ù lyu 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 lyu xing , bi dao da zhong dian geng mei hao

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

echo 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

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

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

$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('重庆');

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

$pinyin->toArray(); 
// [
//     "重": "zhòng",
//     "庆": "qìng"
// ]
bash
php ./bin/pinyin 带着希望去旅行

# dài zhe xī wàng qù lyu xíng