From 13cf8034ec3d7a24c096645cbf70970d93d1115d Mon Sep 17 00:00:00 2001 From: overtrue Date: Mon, 25 Apr 2022 23:02:22 +0800 Subject: [PATCH] Name to abbr resolved #177 --- src/Pinyin.php | 9 ++++++++- tests/PinyinTest.php | 6 ++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Pinyin.php b/src/Pinyin.php index c97471d8..fedb9ae5 100644 --- a/src/Pinyin.php +++ b/src/Pinyin.php @@ -77,9 +77,16 @@ public function abbr(string $string, int|string $delimiter = '', int $option = n [$option, $delimiter] = [$delimiter, '']; } + // 用名字转 abbr + if ($this->hasOption($option, \PINYIN_NAME)) { + $result = $this->name($string, $option); + } else { + $result = $this->convert($string, $option | \PINYIN_NO_TONE); + } + return implode($delimiter, array_map(function ($pinyin) { return \is_numeric($pinyin) || preg_match('/\d+/', $pinyin) ? $pinyin : \mb_substr($pinyin, 0, 1); - }, $this->convert($string, $option | \PINYIN_NO_TONE))); + }, $result)); } public function phrase(string $string, string|int $delimiter = ' ', int|string $option = null): string diff --git a/tests/PinyinTest.php b/tests/PinyinTest.php index 57a2c73a..cba671b5 100755 --- a/tests/PinyinTest.php +++ b/tests/PinyinTest.php @@ -102,6 +102,12 @@ public function testSpecialWordsUsingAbbr() $this->assertEquals('dfscdzz123r', $this->pinyin->abbr('Ⅲ度房室传导阻滞123人', PINYIN_KEEP_ENGLISH | PINYIN_KEEP_NUMBER)); } + // test name to abbr + public function testNamesUsingAbbr() + { + $this->assertEquals('stf', $this->pinyin->abbr('单田芳', \PINYIN_NAME)); + } + // test Polyphone public function testPolyphone() {