PHP code example of overtrue / pinyin
1. Go to this page and download the library: Download overtrue/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/ */
overtrue / pinyin example snippets
use Overtrue\Pinyin\Pinyin;
use Overtrue\Pinyin\ToneStyle;
// 使用字符串
echo Pinyin::sentence('你好', 'none'); // ni hao
echo Pinyin::sentence('你好', 'number'); // ni3 hao3
echo Pinyin::sentence('你好', 'symbol'); // nǐ hǎo
// 使用枚举(推荐)
echo Pinyin::sentence('你好', ToneStyle::NONE); // ni hao
echo Pinyin::sentence('你好', ToneStyle::NUMBER); // ni3 hao3
echo Pinyin::sentence('你好', ToneStyle::SYMBOL); // nǐ hǎo
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;
use Overtrue\Pinyin\ToneStyle;
echo Pinyin::sentence('带着希望去旅行,比到达终点更美好');
// dài zhe xī wàng qù lǚ xíng , bǐ dào dá zhōng diǎn gèng měi hǎo
// 去除声调
echo Pinyin::sentence('带着希望去旅行,比到达终点更美好', ToneStyle::NONE);
// dai zhe xi wang qu lv xing , bi dao da zhong dian geng mei hao
// 保留所有非汉字字符
echo Pinyin::fullSentence('ル是片假名,π是希腊字母', ToneStyle::NONE);
// ル shi pian jia ming ,π shi xi la zi mu
echo Pinyin::permalink('带着希望去旅行'); // dai-zhe-xi-wang-qu-lyu-xing
echo Pinyin::permalink('带着希望去旅行', '.'); // dai.zhe.xi.wang.qu.lyu.xing
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::abbr('CGV电影院', false, true)->join(''); // CGVdyy
Pinyin::nameAbbr('欧阳'); // ['o', 'y']
echo Pinyin::nameAbbr('单单单')->join('-'); // s-d-d
Pinyin::name('单某某'); // ['shàn', 'mǒu', 'mǒu']
Pinyin::name('单某某', 'none'); // ['shan', 'mou', 'mou']
Pinyin::name('单某某', 'none')->join('-'); // shan-mou-mou
Pinyin::passportName('吕小布'); // ['lyu', 'xiao', 'bu']
Pinyin::passportName('女小花'); // ['nyu', 'xiao', 'hua']
Pinyin::passportName('律师'); // ['lyu', 'shi']
$pinyin = Pinyin::heteronym('重庆');
$pinyin['重']; // ["zhòng", "chóng", "tóng"]
$pinyin['庆']; // ["qìng"]
$pinyin->toArray();
// [
// "重": ["zhòng", "chóng", "tóng"],
// "庆": ["qìng"]
// ]
$pinyin = Pinyin::heteronym('重庆重庆', ToneStyle::SYMBOL, true);
// or
$pinyin = Pinyin::heteronymAsList('重庆重庆', ToneStyle::SYMBOL);
$pinyin->toArray();
// [
// ["重" => ["zhòng", "chóng", "tóng"]],
// ["庆" => ["qìng"]],
// ["重" => ["zhòng", "chóng", "tóng"]],
// ["庆" => ["qìng"]]
// ]
$pinyin = Pinyin::chars('重庆');
echo $pinyin['重']; // "zhòng"
echo $pinyin['庆']; // "qìng"
$pinyin->toArray();
// [
// "重": "zhòng",
// "庆": "qìng"
// ]
use Overtrue\Pinyin\Pinyin;
// 使用内存优化策略(默认)
Pinyin::useMemoryOptimized();
$result = Pinyin::sentence('你好世界');
echo $result; // nǐ hǎo shì jiè
// 使用缓存策略
Pinyin::useCached();
// 批量处理时性能更好
foreach ($largeDataset as $text) {
$result = Pinyin::sentence($text);
echo $result . "\n";
}
// 清理缓存(可选)
\Overtrue\Pinyin\Converters\CachedConverter::clearCache();
// 使用智能策略
Pinyin::useSmart();
// 短文本自动优化
$result1 = Pinyin::sentence('你好'); // 跳过长词词典
echo $result1; // nǐ hǎo
// 长文本自动调整
$result2 = Pinyin::sentence($longText); // 加载必要的词典
echo $result2;
// 根据运行环境自动选择最佳策略
Pinyin::useAutoStrategy();
// 获取推荐策略信息
$recommended = \Overtrue\Pinyin\ConverterFactory::recommend();
echo "推荐策略: {$recommended}";
use Overtrue\Pinyin\ConverterFactory;
// 创建特定策略的转换器
$converter = ConverterFactory::make('cached');
$result = $converter->convert('你好世界');
echo $result; // nǐ hǎo shì jiè
// 监控内存使用情况
$initialMemory = memory_get_usage();
$converter->convert('测试文本');
$memoryGrowth = memory_get_usage() - $initialMemory;
echo "内存增长: " . round($memoryGrowth / 1024, 2) . " KB";
// 在应用启动时设置
Pinyin::useMemoryOptimized(); // 默认策略,内存占用最小
// 或在 ServiceProvider 中配置
public function boot()
{
Pinyin::useMemoryOptimized();
}
// 处理大量数据时使用缓存策略
Pinyin::useCached();
$results = [];
foreach ($thousandsOfTexts as $text) {
$results[] = Pinyin::sentence($text);
}
// 处理完成后清理缓存
\Overtrue\Pinyin\Converters\CachedConverter::clearCache();
class ConvertPinyinJob implements ShouldQueue
{
public function handle()
{
// 队列任务中使用智能策略
Pinyin::useSmart();
// 处理任务...
}
}
use Overtrue\Pinyin\ConverterFactory;
// 监控不同策略的内存使用情况
$strategies = ['memory', 'cached', 'smart'];
foreach ($strategies as $strategy) {
$converter = ConverterFactory::make($strategy);
$initialMemory = memory_get_usage();
$converter->convert('测试文本');
$memoryGrowth = memory_get_usage() - $initialMemory;
echo "策略: {$strategy}, 内存增长: " . round($memoryGrowth / 1024, 2) . " KB" . PHP_EOL;
}
echo Pinyin::sentence('旅行');
// lǚ xíng
echo Pinyin::sentence('旅行', 'none');
// lv xing
echo Pinyin::yuToYu()->sentence('旅行', 'none');
// lyu xing
echo Pinyin::yuToU()->sentence('旅行', 'none');
// lu xing
echo Pinyin::yuToV()->sentence('旅行', 'none');
// lv xing
bash
# 运行标准基准测试
php benchmark/run.php
# 详细的策略对比测试
php benchmark/compare-strategies.php
bash
# 运行标准基准测试
php benchmark/run.php
# 详细的策略对比
php benchmark/compare-strategies.php
bash
php ./bin/pinyin 带着希望去旅行 --method=sentence --tone-style=symbol
# dài zhe xī wàng qù lǚ xíng