├── src ├── Pinyin.php ├── PinyinService.php └── helpers.php ├── composer.json ├── LICENSE └── README.md /src/Pinyin.php: -------------------------------------------------------------------------------- 1 | app->bind('pinyin', Pinyin::class); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "xiaodi/think-pinyin", 3 | "description": "ThinkPHP 中文转拼音扩展包", 4 | "license": "MIT", 5 | "authors": [ 6 | { 7 | "name": "xiaodi", 8 | "email": "liangjinbiao@live.com" 9 | } 10 | ], 11 | "keywords": [ 12 | "thinkphp", 13 | "pinyin", 14 | "chinese" 15 | ], 16 | "require": { 17 | "overtrue/pinyin": "~4.0", 18 | "topthink/framework": "6.0.*|5.1.*" 19 | }, 20 | "autoload": { 21 | "psr-4": { 22 | "xiaodi\\ThinkPinyin\\": "src/" 23 | }, 24 | "files": [ 25 | "src/helpers.php" 26 | ] 27 | }, 28 | "extra": { 29 | "think": { 30 | "services": [ 31 | "xiaodi\\ThinkPinyin\\PinyinService" 32 | ] 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 XiaoDi 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. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ThinkPHP-Pinyin 2 | ThinkPHP版 中文转拼音扩展包 支持ThinkPHP `5.1` `6.0`版本 3 | 4 | ## 安装 5 | ```sh 6 | composer require "xiaodi/think-pinyin" 7 | ``` 8 | **使用包** 9 | * [overtrue/pinyin](https://github.com/overtrue/pinyin) `安正超` 10 | 11 | ## 快捷使用 12 | 13 | | 助手函数 | 方法调用 | 14 | | ------------- | --------------------------------------------------- | 15 | | `pinyin()` | `app('pinyin')->convert()` | 16 | | `pinyin_abbr()` | `app('pinyin')->abbr()` | 17 | | `pinyin_permalink()` | `app('pinyin')->permalink()` | 18 | | `pinyin_sentence()` | `app('pinyin')->sentence()` | 19 | 20 | ```php 21 | var_dump(pinyin('带着希望去旅行,比到达终点更美好')); 22 | // ["dai", "zhe", "xi", "wang", "qu", "lv", "xing", "bi", "dao", "da", "zhong", "dian", "geng", "mei", "hao"] 23 | 24 | var_dump(pinyin_abbr('带着希望去旅行,比到达终点更美好')); 25 | // dzxwqlxbddzdgmh 26 | 27 | var_dump(pinyin_permalink('带着希望去旅行,比到达终点更美好')) 28 | // dai-zhe-xi-wang-qu-lyu-xing-bi-dao-da-zhong-dian-geng-mei-hao 29 | 30 | var_dump(pinyin_sentence('带着希望去旅行,比到达终点更美好')) 31 | // daizhexiwangqulyuxing,bidaodazhongdiangengmeihao 32 | ``` 33 | 34 | ## 门面调用 35 | ```php 36 | use xiaodi\ThinkPinyin\Pinyin; 37 | 38 | Pinyin::convert(); 39 | Pinyin::abbr(); 40 | Pinyin::permalink(); 41 | Pinyin::sentence(); 42 | ``` 43 | 44 | ## 控制器调用 45 | 46 | ### 5.1 47 | ```php 48 | use think\Controller; 49 | 50 | class Index extends Controller 51 | { 52 | $this->app->pinyin->convert('带着希望去旅行,比到达终点更美好'); 53 | } 54 | 55 | ``` 56 | 57 | ### 6.0 58 | ```php 59 | use app\BaseController 60 | 61 | class Index extends BaseController 62 | { 63 | $this->app->pinyin->convert('带着希望去旅行,比到达终点更美好'); 64 | } 65 | 66 | ``` 67 | -------------------------------------------------------------------------------- /src/helpers.php: -------------------------------------------------------------------------------- 1 | convert($string, $option); 21 | } 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 string 32 | */ 33 | function pinyin_abbr($string, $delimiter = '') 34 | { 35 | return app('pinyin')->abbr($string, $delimiter); 36 | } 37 | } 38 | 39 | if (!function_exists('pinyin_permlink')) { 40 | /** 41 | * Get a pinyin permalink from string. 42 | * 43 | * @param string $string 44 | * @param string $delimiter 45 | * 46 | * @return string 47 | * 48 | * @deprecated since version 3.0.1. Use the "pinyin_permalink" method instead. 49 | */ 50 | function pinyin_permlink($string, $delimiter = '-') 51 | { 52 | return app('pinyin')->permalink($string, $delimiter); 53 | } 54 | } 55 | 56 | if (!function_exists('pinyin_permalink')) { 57 | /** 58 | * Get a pinyin permalink from string. 59 | * 60 | * @param string $string 61 | * @param string $delimiter 62 | * 63 | * @return string 64 | */ 65 | function pinyin_permalink($string, $delimiter = '-') 66 | { 67 | return app('pinyin')->permalink($string, $delimiter); 68 | } 69 | } 70 | 71 | if (!function_exists('pinyin_sentence')) { 72 | /** 73 | * Get the fist pinyin and letters of given string. 74 | * 75 | * @param string $string 76 | * @param string $tone 77 | * 78 | * @return string 79 | */ 80 | function pinyin_sentence($string, $tone = false) 81 | { 82 | return app('pinyin')->sentence($string, $tone); 83 | } 84 | } 85 | --------------------------------------------------------------------------------