马尾辫全攻略:从类型选择到实操手法,手把手教你扎出精致发型
2026/10/28 22:49:13
# Composer 安装(推荐) composer require overtrue/pinyin参数名 | 类型 | 默认值 | 说明 |
delimiter | string | ''(空字符串) | 拼音分隔符,如'-'结果为zhong-guo |
accent | bool | false | 是否保留拼音声调(如zhōng) |
upper | bool | false | 是否转为全大写 |
lower | bool | true | 是否转为全小写(upper为true时失效) |
keep_unconvertible | bool | false | 是否保留无法转换的字符(如特殊符号、外文) |
mode | int | Pinyin::NORMAL | 拼音转换模式,详见下文 |
// 方法签名 public function convert(string $string, int $mode = null, bool $keepUnconvertible = null): array<?php require __DIR__ . '/vendor/autoload.php'; use Overtrue\Pinyin\Pinyin; // 实例化(默认配置:无分隔符、无声调、小写、过滤无法转换字符) $pinyin = new Pinyin(); $text = "中国加油,世界和平!"; $result1 = $pinyin->convert($text); print_r($result1); // 输出:Array ( [0] => zhong [1] => guo [2] => jia [3] => you [4] => shi [5] => jie [6] => he [7] => ping ) $result2 = $pinyin->permalink($text); echo $result2 . PHP_EOL; // 输出:zhong-guo-jia-you-shi-jie-he-ping $result3 = $pinyin->abbr($text); echo $result3 . PHP_EOL; // 输出:ZGJYSJHP<?php require __DIR__ . '/vendor/autoload.php'; use Overtrue\Pinyin\Pinyin; // 配置:带声调、分隔符为空格、保留无法转换字符 $pinyin = new Pinyin([ 'delimiter' => ' ', 'accent' => true, 'keep_unconvertible' => true, 'mode' => Pinyin::TONE, ]); $text = "重庆(Chongqing)的火锅很好吃!"; $result1 = $pinyin->convert($text); print_r($result1); // 输出:Array ( [0] => chóng [1] => qìng [2] => ( [3] => Chongqing [4] => ) [5] => de [6] => huǒ [7] => guō [8] => hěn [9] => hǎo [10] => chī ) $result2 = $pinyin->permalink($text, '-'); echo $result2 . PHP_EOL; // 输出:chóng-qìng-(-Chongqing-)-de-huǒ-guō-hěn-hǎo-chī<?php require __DIR__ . '/vendor/autoload.php'; use Overtrue\Pinyin\Pinyin; $pinyin = new Pinyin([ 'accent' => true, 'mode' => Pinyin::POLYPHONE, ]); // 多音字:"行"、"乐" $text = "行万里路,读万卷书;知足常乐。"; $result = $pinyin->convert($text); print_r($result); // 输出关键部分: // [0] => Array ( [0] => xíng [1] => háng [2] => hàng [3] => xìng ) ("行"的所有读音) // [10] => Array ( [0] => lè [1] => yuè ) ("乐"的所有读音)