From 39c385d377dd30b51089f1f8ea672d3691985bf1 Mon Sep 17 00:00:00 2001 From: wanghongjun <1445693971@qq,com> Date: Mon, 18 Sep 2023 15:49:30 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=94=E5=9B=9E=E5=AD=97=E6=AE=B5=E6=96=B0?= =?UTF-8?q?=E5=A2=9Epinyin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/lib/pinyin/PinyinNumber.php | 50 ++++++++++++++++++++++++++ app/logic/Zone.php | 11 ++++-- 2 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 app/common/lib/pinyin/PinyinNumber.php diff --git a/app/common/lib/pinyin/PinyinNumber.php b/app/common/lib/pinyin/PinyinNumber.php new file mode 100644 index 0000000..583f5b3 --- /dev/null +++ b/app/common/lib/pinyin/PinyinNumber.php @@ -0,0 +1,50 @@ + 'ling', 1 => 'yi', 2 => 'er', 3 => 'san', 4 => 'si', 5 => 'wu', 6 => 'liu', 7 => 'qi', 8 => 'ba', 9 => 'jiu']; + + protected static $unit = ['', 'shi', 'bai', 'qian', 'wan', 'shiwan', 'baiwan', 'qianwan', 'yi']; + + /** + * 获取数字的拼音 + * @param $number + * @param $strtoupper // 返回大写 + * @return string + */ + public static function getPinyin($number,$strtoupper = false) + { + $number = strval($number); // 将数字转换为字符串 + $length = strlen($number); + $pinyinStr = ''; + + for ($i = 0; $i < $length; $i++) { + $digit = intval($number[$i]); + + if ($digit != 0) { + // 处理万以上的位数 + if ($length - $i > 4) { + $pinyinStr .= self::$pinyin[$digit] . self::$unit[$length - $i - 1]; + // 处理万位之间的零 + if ($digit == 0 && $number[$i + 1] != 0) { + $pinyinStr .= 'ling'; + } + } else { + $pinyinStr .= self::$pinyin[$digit] . self::$unit[$length - $i - 1]; + } + } else { + // 处理连续的0 + if ($i == 0 || $number[$i] != 0) { + $pinyinStr .= self::$pinyin[$digit]; + } + } + } + if ($strtoupper) $pinyinStr = strtoupper($pinyinStr); + return $pinyinStr; + } +} diff --git a/app/logic/Zone.php b/app/logic/Zone.php index ecb388f..976a03d 100644 --- a/app/logic/Zone.php +++ b/app/logic/Zone.php @@ -1,6 +1,7 @@ $v) { + $data[$key][$k]['amount'] = rtrim($v['amount'],'.00'); unset($data[$key][$k]['id']); } } @@ -232,14 +234,17 @@ class Zone break; } } - + $amount = rtrim($selectedPattern['amount'],'.00'); + $pinyin = PinyinNumber::getPinyin(100000,true); $data[$i][] = [ - 'amount' => $selectedPattern['amount'], + 'amount' => $amount, + 'pinyin' => $pinyin, 'image' => get_image_url($selectedPattern['image']) ]; $save_data[$i][] = [ 'id' => $selectedPattern['id'], - 'amount' => $selectedPattern['amount'], + 'amount' => $amount, + 'pinyin' => $pinyin, 'image' => get_image_url($selectedPattern['image']) ]; }