Heim > Artikel > Backend-Entwicklung > PHP实现的汉字拼音转换和公历农历转换类及使用示例_PHP教程
本文整理了PHP汉字拼音转换和公历农历转换两个功能类文件,非常实用。比如我们查找通讯录可以通过联系人姓名的拼音首字母来查询,可以通过首字母来导航大数据量,可以通过转换拼音来做网站优化等。公农历转化一般用在日历日程安排的项目中,方便农历的节日提醒等等。
1、PHP汉字转拼音
Pinyin.class.php类文件可以将大多数汉字转换成汉语拼音,当然也有个别生僻字不能转换,如果你想转换所有的汉字拼音的话,可能需要再配合一个汉字字库来实现,使用该类文件就基本能满足你的项目需求了。用法:
Pinyin.class.php 源码:
function g($num) {
if ($num > 0 && $num
return chr($num);
}
elseif ($num -10247) {
return "";
} else {
for ($i = count($this->d) - 1; $i >= 0; $i--) {
if ($this->d[$i][1]
break;
}
return $this->d[$i][0];
}
}
function c($str) {
$ret = "";
for ($i = 0; $i
$p = ord(substr($str, $i, 1));
if ($p > 160) {
$q = ord(substr($str, ++ $i, 1));
$p = $p * 256 + $q -65536;
}
$ret .= $this->g($p);
}
$ret = substr($ret, 0, 1);
return $ret;
}
function f($str) {
$ret = "";
for ($i = 0; $i
$p = ord(substr($str, $i, 1));
if ($p > 160) {
$q = ord(substr($str, ++ $i, 1));
$p = $p * 256 + $q -65536;
}
$ret .= $this->g($p);
}
return $ret;
}
/*
PHP截取UTF-8编码的中英文字符串
*/
function utf8_substr($str, $from, $len) {
return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,' . $from . '}' . '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,' . $len . '}).*#s', '$1', $str);
}
/*
将中英文字符串转换成拼音
*/
function strtopin($chinese,$type=0) {
$i = 0;
$pinyin = "";
$arr = array ();
while (1) {
$str = $this->utf8_substr($chinese, $i, 1);
if (!empty ($str)) {
$arr[$i] = $str;
$str = iconv("utf-8", "gb2312", $str);
if($type==1){ //转换成首字母
$pinyin .= $this->c($str);
}else{ //转换成全拼
$pinyin .= $this->f($str)." ";
}
$i = $i +1;
} else
break;
}
return $pinyin;
}
}
?>
Lunar.class.php源码:
function convertSolarMonthToLunar($year,$month) {
$yearData = $this->lunarInfo[$year-$this->MIN_YEAR];
if($year==$this->MIN_YEAR&&$month
return array(1891,'正月','初一','辛卯',1,1,'兔');
}
$month_days_ary = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
$dd = $month_days_ary[$month];
if($this->isLeapYear($year) && $month == 2) $dd++;
$lunar_ary = array();
for ($i = 1; $i
$array = $this->getLunarByBetween($year,$this->getDaysBetweenSolar($year, $month, $i, $yearData[1], $yearData[2]));
$array[] = $year . '-' . $month . '-' . $i;
$lunar_ary[$i] = $array;
}
return $lunar_ary;
}
/**
* 将阴历转换为阳历
* @param year 阴历-年
* @param month 阴历-月,闰月处理:例如如果当年闰五月,那么第二个五月就传六月,相当于阴历有13个月,只是有的时候第13个月的天数为0
* @param date 阴历-日
*/
function convertLunarToSolar($year,$month,$date){
$yearData = $this->lunarInfo[$year-$this->MIN_YEAR];
$between = $this->getDaysBetweenLunar($year,$month,$date);
$res = mktime(0,0,0,$yearData[1],$yearData[2],$year);
$res = date('Y-m-d', $res+$between*24*60*60);
$day = explode('-', $res);
$year = $day[0];
$month= $day[1];
$day = $day[2];
return array($year, $month, $day);
}
/**
* 判断是否是闰年
* @param year
*/
function isLeapYear($year){
return (($year%4==0 && $year%100 !=0) || ($year%400==0));
}
/**
* 获取干支纪年
* @param year
*/
function getLunarYearName($year){
$sky = array('庚','辛','壬','癸','甲','乙','丙','丁','戊','己');
$earth = array('申','酉','戌','亥','子','丑','寅','卯','辰','巳','午','未');
$year = $year.'';
return $sky[$year{3}].$earth[$year%12];
}
/**
* 根据阴历年获取生肖
* @param year 阴历年
*/
function getYearZodiac($year){
$zodiac = array('猴','鸡','狗','猪','鼠','牛','虎','兔','龙','蛇','马','羊');
return $zodiac[$year%12];
}
/**
* 获取阳历月份的天数
* @param year 阳历-年
* @param month 阳历-月
*/
function getSolarMonthDays($year,$month){
$monthHash = array('1'=>31,'2'=>$this->isLeapYear($year)?29:28,'3'=>31,'4'=>30,'5'=>31,'6'=>30,'7'=>31,'8'=>31,'9'=>30,'10'=>31,'11'=>30,'12'=>31);
return $monthHash["$month"];
}
/**
* 获取阴历月份的天数
* @param year 阴历-年
* @param month 阴历-月,从一月开始
*/
function getLunarMonthDays($year,$month){
$monthData = $this->getLunarMonths($year);
return $monthData[$month-1];
}
/**
* 获取阴历每月的天数的数组
* @param year
*/
function getLunarMonths($year){
$yearData = $this->lunarInfo[$year - $this->MIN_YEAR];
$leapMonth = $yearData[0];
$bit = decbin($yearData[3]);
for ($i = 0; $i
$bitArray[$i] = substr($bit, $i, 1);
}
for($k=0,$klen=16-count($bitArray);$k
array_unshift($bitArray, '0');
}
$bitArray = array_slice($bitArray,0,($leapMonth==0?12:13));
for($i=0; $i
}
return $bitArray;
}
/**
* 获取农历每年的天数
* @param year 农历年份
*/
function getLunarYearDays($year){
$yearData = $this->lunarInfo[$year-$this->MIN_YEAR];
$monthArray = $this->getLunarYearMonths($year);
$len = count($monthArray);
return ($monthArray[$len-1]==0?$monthArray[$len-2]:$monthArray[$len-1]);
}
function getLunarYearMonths($year){
//debugger;
$monthData = $this->getLunarMonths($year);
$res=array();
$temp=0;
$yearData = $this->lunarInfo[$year-$this->MIN_YEAR];
$len = ($yearData[0]==0?12:13);
for($i=0;$i
$temp=0;
for($j=0;$j
$temp+=$monthData[$j];
}
array_push($res, $temp);
}
return $res;
}
/**
* 获取闰月
* @param year 阴历年份
*/
functio