├── .github ├── FUNDING.yml └── dependabot.yml ├── .gitignore ├── src ├── Facades │ └── Pinyin.php ├── ServiceProvider.php └── helpers.php ├── composer.json ├── LICENSE └── README.md /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [overtrue] 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | composer.lock 2 | vendor 3 | .idea 4 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: composer 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | time: "21:00" 8 | open-pull-requests-limit: 10 9 | -------------------------------------------------------------------------------- /src/Facades/Pinyin.php: -------------------------------------------------------------------------------- 1 | app->singleton(Pinyin::class, function ($app) { 23 | return new Pinyin(); 24 | }); 25 | 26 | $this->app->alias(Pinyin::class, 'pinyin'); 27 | } 28 | 29 | /** 30 | * Get the services provided by the provider. 31 | * 32 | * @return array 33 | */ 34 | public function provides() 35 | { 36 | return [Pinyin::class, 'pinyin']; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "overtrue/laravel-pinyin", 3 | "description": "Chinese to Pinyin translator.", 4 | "keywords": [ 5 | "laravel", 6 | "pinyin", 7 | "chinese", 8 | "overtrue" 9 | ], 10 | "require": { 11 | "php": ">=8.1", 12 | "laravel/framework": "^12.0", 13 | "overtrue/pinyin": "^6.0" 14 | }, 15 | "autoload": { 16 | "psr-4": { 17 | "Overtrue\\LaravelPinyin\\": "src/" 18 | }, 19 | "files": [ 20 | "src/helpers.php" 21 | ] 22 | }, 23 | "extra": { 24 | "laravel": { 25 | "providers": [ 26 | "Overtrue\\LaravelPinyin\\ServiceProvider" 27 | ], 28 | "aliases": { 29 | "Pinyin": "Overtrue\\LaravelPinyin\\Facades\\Pinyin" 30 | } 31 | } 32 | }, 33 | "license": "MIT", 34 | "authors": [ 35 | { 36 | "name": "overtrue", 37 | "email": "anzhengchao@gmail.com" 38 | } 39 | ], 40 | "require-dev": { 41 | "brainmaestro/composer-git-hooks": "dev-master", 42 | "laravel/pint": "^1.5" 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 安正超 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /src/helpers.php: -------------------------------------------------------------------------------- 1 | convert($string, $option); 19 | } 20 | } else { 21 | Log::warning('There exist multiple function "pinyin".'); 22 | } 23 | 24 | if (!function_exists('pinyin_abbr')) { 25 | /** 26 | * Get the fist letters of given string. 27 | * 28 | * @param string $string 29 | * @param string $delimiter 30 | * 31 | * @return \Overtrue\Pinyin\Collection 32 | */ 33 | function pinyin_abbr($string, $delimiter = '') 34 | { 35 | return app(Pinyin::class)->abbr($string)->join($delimiter); 36 | } 37 | } else { 38 | Log::warning('There exist multiple function "pinyin_abbr".'); 39 | } 40 | 41 | if (!function_exists('pinyin_permlink')) { 42 | /** 43 | * Get a pinyin permalink from string. 44 | * 45 | * @param string $string 46 | * @param string $delimiter 47 | * 48 | * @return string 49 | * 50 | * @deprecated since version 3.0.1. Use the "pinyin_permalink" method instead. 51 | */ 52 | function pinyin_permlink($string, $delimiter = '-') 53 | { 54 | return app(Pinyin::class)->permalink($string, $delimiter); 55 | } 56 | } else { 57 | Log::warning('There exist multiple function "pinyin_permlink".'); 58 | } 59 | 60 | if (!function_exists('pinyin_permalink')) { 61 | /** 62 | * Get a pinyin permalink from string. 63 | * 64 | * @param string $string 65 | * @param string $delimiter 66 | * 67 | * @return string 68 | */ 69 | function pinyin_permalink($string, $delimiter = '-') 70 | { 71 | return app(Pinyin::class)->permalink($string, $delimiter); 72 | } 73 | } else { 74 | Log::warning('There exist multiple function "pinyin_permalink".'); 75 | } 76 | 77 | if (!function_exists('pinyin_sentence')) { 78 | /** 79 | * Get the fist pinyin and letters of given string. 80 | * 81 | * @param string $string 82 | * @param string $tone 83 | * 84 | * @return \Overtrue\Pinyin\Collection 85 | */ 86 | function pinyin_sentence($string, $tone = false) 87 | { 88 | return app(Pinyin::class)->sentence($string, $tone); 89 | } 90 | } else { 91 | Log::warning('There exist multiple function "pinyin_sentence".'); 92 | } 93 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel-Pinyin 2 | 3 | Chinese to Pinyin translator for Laravel based on [overtrue/pinyin](https://github.com/overtrue/pinyin). 4 | 5 | [![Latest Stable Version](https://poser.pugx.org/overtrue/laravel-pinyin/v/stable.svg)](https://packagist.org/packages/overtrue/laravel-pinyin) [![Total Downloads](https://poser.pugx.org/overtrue/laravel-pinyin/downloads.svg)](https://packagist.org/packages/overtrue/laravel-pinyin) [![Latest Unstable Version](https://poser.pugx.org/overtrue/laravel-pinyin/v/unstable.svg)](https://packagist.org/packages/overtrue/laravel-pinyin) [![License](https://poser.pugx.org/overtrue/laravel-pinyin/license.svg)](https://packagist.org/packages/overtrue/laravel-pinyin) 6 | 7 | [![Sponsor me](https://github.com/overtrue/overtrue/blob/master/sponsor-me-button-s.svg?raw=true)](https://github.com/sponsors/overtrue) 8 | 9 | ## Install 10 | 11 | ```shell 12 | composer require "overtrue/laravel-pinyin:^6.0" 13 | ``` 14 | 15 | The auto-discovery feature will handle these two steps for you. 16 | 17 | Add the following line to the section `providers` of `config/app.php`: 18 | 19 | ```php 20 | 'providers' => [ 21 | //... 22 | Overtrue\LaravelPinyin\ServiceProvider::class, 23 | ], 24 | ``` 25 | 26 | as optional, you can use facade: 27 | 28 | ```php 29 | 30 | 'aliases' => [ 31 | //... 32 | 'Pinyin' => Overtrue\LaravelPinyin\Facades\Pinyin::class, 33 | ], 34 | ``` 35 | 36 | ## Usage 37 | 38 | you can get the instance of `Overtrue\Pinyin\Pinyin` from app container: 39 | 40 | ```php 41 | 42 | $pinyin = app('pinyin'); 43 | echo $pinyin->sentence('带着希望去旅行,比到达终点更美好'); 44 | // dài zhe xī wàng qù lǔ xíng, bǐ dào dá zhōng diǎn gèng měi hǎo 45 | ``` 46 | 47 | There are more convenient functions: 48 | 49 | | function | method | 50 | | ------------- | --------------------------------------------------- | 51 | | `pinyin()` | `app('pinyin')->convert()` | 52 | | `pinyin_abbr()` | `app('pinyin')->abbr()` | 53 | | `pinyin_permalink` | `app('pinyin')->permalink()` | 54 | | `pinyin_sentence` | `app('pinyin')->sentence()` | 55 | 56 | ```php 57 | var_dump(pinyin('带着希望去旅行,比到达终点更美好')); 58 | // ["dai", "zhe", "xi", "wang", "qu", "lv", "xing", "bi", "dao", "da", "zhong", "dian", "geng", "mei", "hao"] 59 | 60 | var_dump(pinyin_abbr('带着希望去旅行')); 61 | // dzxwqlx 62 | ... 63 | ``` 64 | 65 | Using facade: 66 | 67 | ```php 68 | use Pinyin; // Facade class, NOT Overtrue\Pinyin\Pinyin 69 | 70 | var_dump(Pinyin::convert('带着希望去旅行')); 71 | // ["dai", "zhe", "xi", "wang", "qu", "lv", "xing"] 72 | 73 | echo Pinyin::sentence('带着希望去旅行,比到达终点更美好'); 74 | // dài zhe xī wàng qù lǔ xíng, bǐ dào dá zhōng diǎn gèng měi hǎo 75 | 76 | ``` 77 | 78 | About `overtrue/pinyin` specific configuration and use, refer to: [overtrue/pinyin](https://github.com/overtrue/pinyin) 79 | 80 | ## Performance Strategies (New in 6.0) 81 | 82 | Laravel-pinyin 6.0 includes support for the new performance optimization strategies introduced in overtrue/pinyin 6.0: 83 | 84 | ```php 85 | use Overtrue\Pinyin\Pinyin; 86 | 87 | // Memory Optimized (default) - ~400KB memory usage, suitable for web requests 88 | Pinyin::useMemoryOptimized(); 89 | 90 | // Cached Strategy - ~4MB memory usage, 2-3x faster for repeated conversions 91 | Pinyin::useCached(); 92 | 93 | // Smart Strategy - 600KB-1.5MB memory usage, adaptive loading 94 | Pinyin::useSmart(); 95 | 96 | // Auto Strategy - automatically selects the best strategy for your environment 97 | Pinyin::useAutoStrategy(); 98 | 99 | // Clear cache when needed (useful in long-running processes) 100 | Pinyin::clearCache(); 101 | ``` 102 | 103 | ## :heart: Sponsor me 104 | 105 | [![Sponsor me](https://github.com/overtrue/overtrue/blob/master/sponsor-me.svg?raw=true)](https://github.com/sponsors/overtrue) 106 | 107 | 如果你喜欢我的项目并想支持它,[点击这里 :heart:](https://github.com/sponsors/overtrue) 108 | 109 | 110 | ## Project supported by JetBrains 111 | 112 | Many thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects. 113 | 114 | [![](https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.svg)](https://www.jetbrains.com/?from=https://github.com/overtrue) 115 | 116 | ## PHP 扩展包开发 117 | 118 | > 想知道如何从零开始构建 PHP 扩展包? 119 | > 120 | > 请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— [《PHP 扩展包实战教程 - 从入门到发布》](https://learnku.com/courses/creating-package) 121 | 122 | ## License 123 | 124 | MIT 125 | --------------------------------------------------------------------------------