From 1350518c8211332de2f9c40d88b6e290b383bd31 Mon Sep 17 00:00:00 2001 From: ssynhtn Date: Fri, 11 Jan 2019 20:52:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=80=E4=B8=AAmultibyte?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=BA=A7=E7=94=9F=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20(#118)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 修复一个multibyte字符产生的问题 * 修复一个代码风格问题 --- src/Pinyin.php | 2 +- tests/AbstractDictLoaderTestCase.php | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Pinyin.php b/src/Pinyin.php index 1055fcc4..efab9b77 100644 --- a/src/Pinyin.php +++ b/src/Pinyin.php @@ -143,7 +143,7 @@ public function abbr($string, $delimiter = '', $option = PINYIN_DEFAULT) } return implode($delimiter, array_map(function ($pinyin) { - return \is_numeric($pinyin) ? $pinyin : $pinyin[0]; + return \is_numeric($pinyin) ? $pinyin : mb_substr($pinyin, 0, 1); }, $this->convert($string, $option))); } diff --git a/tests/AbstractDictLoaderTestCase.php b/tests/AbstractDictLoaderTestCase.php index 9e9b7c2a..0794397f 100644 --- a/tests/AbstractDictLoaderTestCase.php +++ b/tests/AbstractDictLoaderTestCase.php @@ -11,10 +11,14 @@ namespace Overtrue\Pinyin\Test; +use Overtrue\Pinyin\Pinyin; use PHPUnit_Framework_TestCase; abstract class AbstractDictLoaderTestCase extends PHPUnit_Framework_TestCase { + /** + * @var Pinyin + */ protected $pinyin; public function testConvert() @@ -111,6 +115,13 @@ public function testSpecialWords() $this->assertEquals('zhōng dù xìng fèi shuǐ zhǒng', $pinyin->sentence('中度性肺水肿', \PINYIN_TONE)); } + // test multibyte non-Chinese character + public function testSpecialWordsUsingAbbr() + { + $pinyin = $this->pinyin; + $this->assertEquals('Ⅲdfscdzz', $pinyin->abbr('Ⅲ度房室传导阻滞', PINYIN_KEEP_ENGLISH | PINYIN_KEEP_NUMBER)); + } + // test Polyphone public function testPolyphone() {