PHP常用函数

本文介绍了一系列实用的PHP函数,包括数字转换、随机数生成、字符串处理等,旨在帮助开发者提高编程效率。文章详细解释了如何将数字转换为大写形式、生成不重复随机数、处理字符串以及获取特定日期信息的方法。
//转大写数字 最大99
function daxie($number){
    $number=substr($number,0,2);
    $arr=array("","","","","","","","","","");
    if(strlen($number)==1){
        $result=$arr[$number];
    }else{
        if($number==10){
            $result="";
        }else{
            if($number<20){
                $result="";
            }else{
                $result=$arr[substr($number,0,1)]."";
            }
            if(substr($number,1,1)!="0"){
                $result.=$arr[substr($number,1,1)];
            }
        }
    }
    return $result;
}

/**
 * 生成不重复的随机数
 * @param  int $start  需要生成的数字开始范围
 * @param  int $end    结束范围
 * @param  int $length 需要生成的随机数个数
 * @return array       生成的随机数
 */
function get_rand_number($start=1,$end=10,$length=4){
    $connt=0;
    $temp=array();
    while($connt<$length){
        $temp[]=mt_rand($start,$end);
        $data=array_unique($temp);
        $connt=count($data);
    }
    sort($data);
    return $data;
}



//汉字转数组
function ch2arr($str)
{
    $length = mb_strlen($str, 'utf-8');
    $array = [];
    for ($i=0; $i<$length; $i++)  
        $array[] = mb_substr($str, $i, 1, 'utf-8');    
    return $array;
}



//获取全年每月的头尾日期
function getOneYearAllMonth($year = null)
{
   if ($year == null) {
      $year = date('Y');
   }

   //首先判断闰年
   if($year%400 == 0  || ($year%4 == 0 && $year%100 !== 0)){
      $rday = 29;
   }else{
      $rday = 28;
   }
   for ($i=1; $i<=12;$i++){
      if($i==2){
         $days = $rday;
      }else{
         //判断是大月(31),还是小月(30         $days = (($i - 1)%7%2) ? 30 : 31;
      }

      if($i < 10)
      {
         $mon = "0".$i;
      }
      else
      {
         $mon = $i;
      }
      
      
      $mouth[] = array(
            'month' => $i,
            'start' => $year.$mon."01",
            'end' => $year.$mon.$days,
      );

      //echo $year."".$i."月有:".$days.""."<br>";
   }
   return $mouth;
}




/**
 * 判断某年的某月有多少天
 * @return [type] [description]
 */
function daysInmonth($year='',$month='')
{
   if(empty($year))
   {
      $year = date('Y');
   }
   if(empty($month))
   { 
      $month = date('m');
   }
   
   if (in_array($month, array(1, 3, 5, 7, 8, '01', '03', '05', '07', '08', 10, 12))) 
   {
      $text = '31';        //月大
   }
   elseif ($month == 2 || $month == '02')
   {
      //判断是否是闰年
      if ( ($year % 400 == 0) || ( ($year % 4 == 0) && ($year % 100 !== 0) ) ) 
      {   
         $text = '29';        //闰年2      }
      else 
      {
         $text = '28';        //平年2      }
   } 
   else 
   {
      $text = '30';            //月小
   }
   return intval($text);
}



/**
 * [yuan_img 编辑图片为圆形]  剪切头像为圆形
 * @param  [string] $imgpath [头像保存之后的图片名]
 */
function yuan_img($imgpath) {
    $ext     = pathinfo($imgpath);
    $src_img = null;
    switch ($ext['extension']) {
        case 'jpg':
            $src_img = imagecreatefromjpeg($imgpath);
            break;
        case 'png':
            $src_img = imagecreatefromjpeg($imgpath);
            break;
    }
    $wh  = getimagesize($imgpath);
    $w   = $wh[0];
    $h   = $wh[1];
    $w   = min($w, $h);
    $h   = $w;
    $img = imagecreatetruecolor($w, $h);
    //这一句一定要有
    imagesavealpha($img, true);
    //拾取一个完全透明的颜色,最后一个参数127为全透明
    $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
    imagefill($img, 0, 0, $bg);
    $r   = $w / 2; //圆半径
    $y_x = $r; //圆心X坐标
    $y_y = $r; //圆心Y坐标
    for ($x = 0; $x < $w; $x++) {
        for ($y = 0; $y < $h; $y++) {
            $rgbColor = imagecolorat($src_img, $x, $y);
            if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
                imagesetpixel($img, $x, $y, $rgbColor);
            }
        }
    }
    return $img;
}




//随机字符串
private function createNonceStr($length = 16) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $str = "";
    for ($i = 0; $i < $length; $i++) {
        $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
    }
    return $str;
}



//http
function httpGet($url)
{
    $curl = curl_init ();                          // 启动一个CURL会话
    curl_setopt ( $curl, CURLOPT_URL, $url );
    curl_setopt ( $curl, CURLOPT_HEADER, 0 );
    curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false );  // 跳过证书检查
    curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, false );  // 从证书中检查SSL加密算法是否存在
    $tmpInfo = curl_exec ( $curl );                   // 返回apijson对象
    // 关闭URL请求
    curl_close ( $curl );
    return $tmpInfo;                              // 返回json对象
}
/**
 * 文字居中
 * @param $text
 */
public static function middle($width,$text,$fontSize) {

    $len = mb_strlen($text,"UTF-8");
    $fix = ($len != 1) ? $len*5 : 4; //比例要与图片实际宽度对应

    return ceil(($width-$len*$fontSize)/2) - $fix;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值