php自備函數(shù)

<pre>
/**

  • 判斷是否SSL協(xié)議
  • @return boolean
    */
    function is_ssl() {
    if(isset($_SERVER['HTTPS']) && ('1' == $_SERVER['HTTPS'] || 'on' == strtolower($_SERVER['HTTPS']))){
    return true;
    }elseif(isset($_SERVER['SERVER_PORT']) && ('443' == $_SERVER['SERVER_PORT'] )) {
    return true;
    }
    return false;
    //真為https假為http
    }
    </pre>

<pre>
//http請求
function https_request($url, $data = null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
</pre>
<pre>
//計算時間多少秒之前
function format_date($time){
$time = strtotime($time);
$t=time()-$time;
$f=array(
'31536000'=>'年',
'2592000'=>'個月',
'604800'=>'星期',
'86400'=>'天',
'3600'=>'小時',
'60'=>'分鐘',
'1'=>'秒'
);
foreach ($f as $k=>$v) {
if (0 !=$c=floor($t/(int)$k)) {
return $c.$v.'前';
}
}
}
</pre>
<pre>
/**

  • 計算距離多少km
  • @param [float] $s [距離 :128.9]
  • @return [string] [200m 100km >30km]
    */
    function convertMeter($s){
    if($s /1000 <1){
    return sprintf("%.0f",$s).'m';
    }elseif($s /1000 >=1 && ($s /1000 < 30)){
    return sprintf("%.2f", ($s /1000))."km";
    }elseif($s /1000 > 30){
    return ">30km";
    }
    }
    </pre>

<pre>
/**
*求兩個已知經(jīng)緯度之間的距離,單位為米
*@param lng1,lng2 經(jīng)度
*@param lat1,lat2 緯度
*@return float 距離,單位米
*@author www.Alixixi.com
**/
//function getdistance($lng1,$lat1,$lng2,$lat2){
function getdistance($in_location,$coordination){
$in_location = explode(',',$in_location);
list($lng1, $lat1) = $in_location;
$coordination = explode(',',$coordination);
list($lng2, $lat2) = $coordination;

//將角度轉(zhuǎn)為狐度
$radLat1=deg2rad($lat1);//deg2rad()函數(shù)將角度轉(zhuǎn)換為弧度
$radLat2=deg2rad($lat2);
$radLng1=deg2rad($lng1);
$radLng2=deg2rad($lng2);
$a=$radLat1-$radLat2;
$b=$radLng1-$radLng2;
$s=2*asin(sqrt(pow(sin($a/2),2)+cos($radLat1)*cos($radLat2)*pow(sin($b/2),2)))*6378.137*1000;
//$s = convertMeter($s);
return $s;

}

</pre>

<pre>
/**
*獲取html文本里的img

  • @param string $content
  • @return array
    */
    function sp_getcontent_imgs($content){
    import("phpQuery");
    \phpQuery::newDocumentHTML($content);
    $pq=pq();
    $imgs=$pq->find("img");
    $imgs_data=array();
    if($imgs->length()){
    foreach ($imgs as $img){
    $img=pq($img);
    $im['src']=$img->attr("src");
    $im['title']=$img->attr("title");
    $im['alt']=$img->attr("alt");
    $imgs_data[]=$im;
    }
    }
    \phpQuery::$documents=null;
    return $imgs_data;
    }
    </pre>

<pre>
/**

  • 保存數(shù)組變量到php文件
    /
    function sp_save_var($path,$value){
    $ret = file_put_contents($path, "<?php\treturn " . var_export($value, true) . ";?>");
    return $ret;
    }
    </pre>
    <pre>
    /
    *
  • 隨機字符串生成
  • @param int $len 生成的字符串長度
  • @return string
    */
    function sp_random_string($len = 6,$type=1) {
    if($type){
    $chars = array(
    "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
    "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
    "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
    "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
    "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
    "3", "4", "5", "6", "7", "8", "9"
    );
    $charsLen = count($chars) - 1;
    shuffle($chars); // 將數(shù)組打亂
    $output = "";
    for ($i = 0; $i < $len; $i++) {
    $output .= $chars[mt_rand(0, $charsLen)];
    }
    }else{
    return mt_rand(1000, 9999);
    }
    return $output;
    }
    </pre>

<pre>
/**

  • 遞歸檢測一個值是否存在

  • @param [string] $value [要查找的字符串]

  • @param [array] $array [檢查所在的數(shù)組]

  • @return [bool] [true/false]
    */
    function deep_in_array($value, $array) {
    foreach($array as $item) {
    if(!is_array($item)) {//如果不是數(shù)組
    if ($item == $value) {
    return true;
    } else {
    continue;
    }
    }

     if(in_array($value, $item)) {  //如果是數(shù)組
         return true;      
     } else if(deep_in_array($value, $item)) {  //遞歸調(diào)用
         return true;      
     }  
    

    }
    return false;
    }
    </pre>

<pre>
/**

  • 獲取團隊人數(shù)

  • @param [string] $name 介紹人姓名

  • @param [int] $rank 獲取下級幾級

  • @param [bool] $activation 是否獲取激活的會員

  • @return [type] [description]
    */
    function getTeam($name,$rank=26,$activation=false,$lev=0){
    global $num ;
    $num++;
    if(!$name) return false;
    if($activation){//獲取激活的下級會員
    $userList = M('member')->field('id,account,active')->where(array('references'=>$name,'active'=>1))->select();
    }else{
    $userList = M('member')->field('id,account,active')->where(array('references'=>$name))->select();
    }
    if($userList && $num<=$rank){
    foreach ($userList as $key => $value) {
    $userList[$key]['lev'] = $num;
    $userList['list'] = getTeam($value['account'],$rank,$activation);
    $arr[] = $userList;
    }
    }
    return $arr;
    }
    </pre>
    <pre>
    function arr_to_xml($arr, $dom = 0, $item = 0) {
    if (! $dom) {
    $dom = new DOMDocument ("1.0","UTF-8");
    }

    if (! $item) {
    $ccc = array_keys ( $arr );
    if ($ccc [0] == 'envelope') {
    $str_head = 'request';
    } else {
    $str_head = 'envelope';
    }
    $item = $dom->createElement ( $str_head );
    $dom->appendChild ( $item );
    }
    foreach ( $arr as $key => $val ) {
    $itemx = $dom->createElement ( is_string ( $key ) ? $key : "record" );
    $item->appendChild ( $itemx );
    if (! is_array ( $val )) {
    $text = $dom->createTextNode ( $val );
    $itemx->appendChild ( $text );
    } else {
    arr_to_xml ( $val, $dom, $itemx );
    }
    }

    return $dom->saveXML ();
    }

function xml_to_array($xml) {
$reg = "/<(\w+)[^>]?>([\x00-\xFF]?)<\/\1>/";
if (preg_match_all ( $reg, $xml, $matches )) {
$count = count ( $matches [0] );
$arr = array ();
for($i = 0; $i < $count; $i ++) {

        $key = $matches [1] [$i];   
        
        $val = xml_to_array ( $matches [2] [$i] ); // 遞歸
        if (array_key_exists ( $key, $arr )) {
            if (is_array ( $arr [$key] )) {
                if (! array_key_exists ( 0, $arr [$key] )) {
                    $arr [$key] = array (
                            $arr [$key] 
                    );
                }
            } else {
                
                $arr [$key] = array (
                        $arr [$key] 
                );
            }
            $arr [$key] [] = $val;
        } else {
            
            $arr [$key] = $val;
        }
    }
    return $arr;
} else {
    return $xml;
}

}
</pre>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容