Skip to content

Commit

Permalink
Name to abbr resolved #177
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Apr 25, 2022
1 parent 339bfa4 commit 13cf803
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Pinyin.php
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions tests/PinyinTest.php
Expand Up @@ -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()
{
Expand Down

0 comments on commit 13cf803

Please sign in to comment.