一些常用的函数 (排列,组合,获取http和https中的内容 )

<?php
function factorial($n) {
  return array_product(range(1, $n));
}

// 排列数
function A_($n, $m) {
  return factorial($n)/factorial($n-$m);
}

// 组合数
function C_($n, $m) {
  return A_($n, $m)/factorial($m);
}

// 排列
function arrangement($a, $m) {
  $r = array();

  $n = count($a);
  if ($m <= 0 || $m > $n) {
    return $r;
  }

  for ($i=0; $i<$n; $i++) {
    $b = $a;
    $t = array_splice($b, $i, 1);
    if ($m == 1) {
      $r[] = $t;
    } else {
      $c = arrangement($b, $m-1);
      foreach ($c as $v) {
        $r[] = array_merge($t, $v);
      }
    }
  }

  return $r;
}

// 组合
function combination($a, $m) {
  $r = array();

  $n = count($a);
  if ($m <= 0 || $m > $n) {
    return $r;
  }

  for ($i=0; $i<$n; $i++) {
    $t = array($a[$i]);
    if ($m == 1) {
      $r[] = $t;
    } else {
      $b = array_slice($a, $i+1);
      $c = combination($b, $m-1);
      foreach ($c as $v) {
        $r[] = array_merge($t, $v);
      }
    }
  }

  return $r;
}

//获取http中的内容
function getJson($url){

    $ch = curl_init();

 

    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $output = curl_exec($ch);

    curl_close($ch);

    return json_decode($output, true);

}

//写入文件
function wjson($data,$file){
    $json_string = json_encode($data);
    file_put_contents("./json/".$file, $json_string);
}

//读取文件
function rjson($file){
    $json_string = file_get_contents("./json/".$file);
    $data = json_decode($json_string, true);
    return $data;
}
function imgstrbuild($data){
        $arr = explode(',', $data);
        $str = "";
        foreach ($arr as $v) {
            $str.="<img class='numimg' src='/Public/images/".$v.".png'/>";
        }
        return $str;

    }

//截取
    function msubstr($str, $start = 0, $length, $charset = "utf-8", $suffix = true)
{
    if (function_exists("mb_substr"))
        $slice = mb_substr($str, $start, $length, $charset);
    elseif (function_exists('iconv_substr')) {
        $slice = iconv_substr($str, $start, $length, $charset);
        if (false === $slice) {
            $slice = '';
        }
    } else {
        $re['utf-8'] = "/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef][x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/";
        $re['gb2312'] = "/[x01-x7f]|[xb0-xf7][xa0-xfe]/";
        $re['gbk'] = "/[x01-x7f]|[x81-xfe][x40-xfe]/";
        $re['big5'] = "/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/";
        preg_match_all($re[$charset], $str, $match);
        $slice = join("", array_slice($match[0], $start, $length));
    }
    //字数不满不添加...
    $count = mb_strlen($str, 'utf-8');
    if ($count > $length) {
        return $suffix ? $slice . '...' : $slice;
    } else {
        return $slice;
    }
}

//获取https中是内容
function get($url)
{
    $ch = curl_init();
    $header[]='Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8';
    // $header[]=
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0');
    $result = curl_exec($ch);
    curl_close($ch);
    return json_decode($result);
}

//输入年月,输出该月开始和结束的时间戳

function mFristAndLast($y="",$m=""){
     if($y=="") $y=date("Y");
     if($m=="") $m=date("m");
     $m=sprintf("%02d",intval($m));
     $y=str_pad(intval($y),4,"0",STR_PAD_RIGHT);
    
     $m>12||$m<1?$m=1:$m=$m;
     $firstday=strtotime($y.$m."01000000");
     $firstdaystr=date("Y-m-01",$firstday);
     $lastday = strtotime(date('Y-m-d 23:59:59', strtotime("$firstdaystr +1 month -1 day")));
     return array("firstday"=>$firstday,"lastday"=>$lastday);
    }



?>

原文地址:https://www.cnblogs.com/nnhgd/p/7954276.html