This article has compiled two functional files for PHP Chinese character pinyin conversion and Gregorian and lunar calendar conversion, which are very practical. For example, when we search the address book, we can search by the pinyin initials of the contact's name, we can navigate large amounts of data by the initials, and we can do website optimization by converting pinyin. Public lunar calendar conversion is generally used in calendar scheduling projects to facilitate lunar calendar festival reminders, etc.
1. Convert Chinese characters to Pinyin with PHP
The Pinyin.class.php class file can convert most Chinese characters into Chinese Pinyin. Of course, there are some rare characters that cannot be converted. If you want to convert all Chinese characters into Pinyin, you may need to use another Chinese character library to achieve it. Use this Class files can basically meet your project needs. Usage:
Pinyin.class.php source code:
function g($num) {
if ($num > 0 && $num < 160) {
return chr($num);
}
elseif ($num < -20319 || $num > -10247) {
return "";
} else {
for ($i = count($this->d) - 1; $i >= 0; $i--) {
if ($this->d[$i][1] <= $num)
break;
}
return $this->d[$i][0];
}
}
function c($str) {
$ret = "";
for ($i = 0; $i < strlen($str); $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 < strlen($str); $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 intercepts UTF-8 encoded Chinese and English strings
*/
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);
}
/*
Convert Chinese and English strings into Pinyin
*/
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){ //Convert to initial letter
$pinyin .= $this->c($str);
}else{ //Convert to full spelling
$pinyin .= $this->f($str)." ";
}
$i = $i 1;
} else
Break;
}
Return $pinyin;
}
}
?>
Lunar.class.php source code:
function convertSolarMonthToLunar($year,$month) {
$yearData = $this->lunarInfo[$year-$this->MIN_YEAR];
if($year==$this->MIN_YEAR&&$month<=2&&$date<=9){
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 < $dd; $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;
}
/**
* Convert lunar calendar to solar calendar
* @param year lunar calendar-year
* @param month Lunar calendar - month, leap month processing: For example, if there is a leap month in May, then the second May will be June, which is equivalent to 13 months in the lunar calendar, but sometimes the number of days in the 13th month is 0
* @param date lunar calendar-day
*/
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);
}
/**
* Determine whether it is a leap year
* @param year
*/
function isLeapYear($year){
return (($year%4==0 && $year0 !=0) || ($year@0==0));
}
/**
* Get the zodiac signs
* @param year
*/
function getLunarYearName($year){
$sky = array('庚','辛','壬','癸','甲','乙','丙','丁','戊','己');
$earth = array('申','酉','戌','亥','子','丑','寅','卯','辰','巳','午','未');
$year = $year.'';
return $sky[$year{3}].$earth[$year];
}
/**
* Get the zodiac sign according to the lunar year
* @param year lunar year
*/
function getYearZodiac($year){
$zodiac = array('猴','鸡','狗','猪','鼠','牛','虎','兔','龙','蛇','马','羊');
return $zodiac[$year];
}
/**
* Get the number of days in the solar calendar month
* @param year Gregorian calendar-year
* @param month Gregorian calendar-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"];
}
/**
* Get the number of days in the lunar month
* @param year lunar calendar-year
* @param month lunar calendar month, starting from January
*/
function getLunarMonthDays($year,$month){
$monthData = $this->getLunarMonths($year);
return $monthData[$month-1];
}
/**
* * Get the array of days in each month of the lunar calendar
* @param year
*/
function getLunarMonths($year){
$yearData = $this->lunarInfo[$year - $this->MIN_YEAR];
$leapMonth = $yearData[0];
$bit = decbin($yearData[3]);
for ($i = 0; $i < strlen($bit);$i ) {
$bitArray[$i] = substr($bit, $i, 1);
}
for($k=0,$klen=16-count($bitArray);$k<$klen;$k ){
array_unshift($bitArray, '0');
}
$bitArray = array_slice($bitArray,0,($leapMonth==0?12:13));
for($i=0; $i
}
return $bitArray;
}
/**
* Get the number of days in each year of the lunar calendar
* @param year lunar 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<$len;$i ){
$temp=0;
for($j=0;$j<=$i;$j ){
$temp =$monthData[$j];
}
array_push($res, $temp);
}
return $res;
}
/**
* Get leap month
* @param year lunar year
*/
functio