PHP code example of yuzhiwei / pinyin

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

    

yuzhiwei / pinyin example snippets



//没有声调
$testStr = $pinyin->transformWithoutTone("你好,中国");

var_dump($testStr); //string(13) "nihaozhongguo"

//汉字首字母
$testStr1 = $pinyin->transformUcwords("中国");

var_dump($testStr1);  //string(2) "ZG"

$testStr1 = $pinyin->transformFirstUcwords("北京");

var_dump($testStr1);  //string(2) "B"

//有声调
$testStr2 = $pinyin->transformWithTone("你好,中国");

var_dump($testStr2);  //string(24) "nǐ hǎo ,zhōng guó "

//获取英文名首字母
$testStr3 = $pinyin->getFirstCharacter('peter pang');
var_dump($testStr3);  //string(2) "PP"

//获取中文名
$testStr4 = $pinyin->getFirstName("吴彦祖");
var_dump($testStr4);   //string(6) "彦祖"