Pinyin.class.phphao hao xue xi tian tian xiang shanghhxxttxsrequire_once 'Pinyin.class.php'; $pinyin = new Pinyin(); $str = '好好學習天天向上'; echo $pinyin->strtopin($str); // 全拼 echo $pinyin->strtopin($str, 1); // 首字母
$d 靜態數組:拼音與 GB2312 區位碼映射(約 6000+ 條)g($num):根據區位碼二分查找對應拼音c($str):取單字首字母f($str):取單字全拼utf8_substr():按字符截取 UTF-8 字符串strtopin():主入口,逐字轉換並拼接class Pinyin {
private $d = [
['a', -20319], ['ai', -20317], ['an', -20304], /* ... 6000+ 條 ... */
];
function g($num) { /* 區位碼查找邏輯 */ }
function c($str) { /* 首字母邏輯 */ }
function f($str) { /* 全拼邏輯 */ }
function utf8_substr($str, $from, $len) { /* UTF-8 安全截取 */ }
function strtopin($chinese, $type = 0) { /* 主方法 */ }
}
Lunar.class.phprequire_once 'Lunar.class.php';
$lunar = new Lunar();
// 陽歷轉陰歷:2013-09-21 → 2013年七月十六
$res = $lunar->convertSolarToLunar(2013, 9, 21);
print_r($res);
/*
Array(
[0] => 二零一三
[1] => 七月
[2] => 十六
[3] => 癸巳
[4] => 7
[5] => 16
[6] => 蛇
[7] => 0
)
*/
// 陰歷轉陽歷:2013年閏九月初十
$res = $lunar->convertLunarToSolar(2013, 10, 10); // 閏九月傳 10
print_r($res); // Array([0]=>2013 [1]=>11 [2]=>13)
$lunarInfo 陣列:每年編碼信息(閏月、正月初一陽歷日期、月份大小位掩碼)getDaysBetweenSolar():計算目標日期與正月初一相差天數getLunarByBetween():根據天數差反推陰歷年月日class Lunar {
var $MIN_YEAR = 1891;
var $MAX_YEAR = 2100;
var $lunarInfo = [ /* 210 條年份編碼 */ ];
function convertSolarToLunar($y, $m, $d) { /* 陽轉陰 */ }
function convertLunarToSolar($y, $m, $d) { /* 陰轉陽 */ }
function getLunarMonths($year) { /* 該年各月天數數組 */ }
function getLeapMonth($year) { /* 閏月 */ }
function getLunarYearName($year) { /* 干支年 */ }
function getYearZodiac($year) { /* 生肖 */ }
function getCapitalNum($num, $isMonth) { /* 數字轉中文:初一、廿三、正月、臘月 */ }
// ... 其他輔助方法
}
$lunarInfo 陣列overtrue/laravel-festival、kitetail/calendars)兩個類庫滿足基礎業務需求:拼音類適合搜索索引、首字母導航、URL 優化;農歷類適合日曆、節日提醒、傳統節氣展示。生產環境建議:
overtrue/pinyin(基於詞庫、支持多音字)kitetail/calendars 或 php-solar-lunar(持續維護、單元測試覆蓋)