美文网首页
php自备函数

php自备函数

作者: 上街买菜丶迷倒老太 | 来源:发表于2016-08-08 10:06 被阅读0次

<pre>
/**

  • 判断是否SSL协议
  • @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>
/**
*求两个已知经纬度之间的距离,单位为米
*@param lng1,lng2 经度
*@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;

//将角度转为狐度
$radLat1=deg2rad($lat1);//deg2rad()函数将角度转换为弧度
$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>
/**

  • 保存数组变量到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); // 将数组打乱
    $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 [检查所在的数组]

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

     if(in_array($value, $item)) {  //如果是数组
         return true;      
     } else if(deep_in_array($value, $item)) {  //递归调用
         return true;      
     }  
    

    }
    return false;
    }
    </pre>

<pre>
/**

  • 获取团队人数

  • @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>

相关文章

  • php自备函数

    /** 判断是否SSL协议 @return boolean*/function is_ssl() {if(isse...

  • PHP参考手册

    PHP参考手册 PHP array() 函数 PHP array_change_key_case()函数 PHP ...

  • PHP中一些函数方法

    php自定义函数之递归函数 php自定义函数之静态变量 php​ 使用系统内置函数 亚麻跌”是PHP学习时间处理的...

  • 从0到1学习网络安全 【PHP基础-PHP 函数】

    PHP 函数 PHP 的真正力量来自它的函数:它拥有超过 1000 个内建的函数。 PHP 用户定义函数 除了内建...

  • 三. PHP与MySQL关系大揭秘

    PHP内置MySQL函数学习(1) PHP内置MySQL函数学习(2) PHP内置MySQL函数学习(2)

  • 九月四号

    PHP函数之内置函数 内置函数指的是PHP默认支持的函数,PHP内置了很多标准的常用的处理函数,包括字符串处理、数...

  • 搜藏经典

    PHP 16个魔术方法 PHP 数组函数 PHP 字符串函数 PHP 超全局变量 PHP 面向对象的理解以及三大特...

  • PHP性能优化

    1 PHP语言级性能优化:少些代码,多用PHP自身能力;(多用PHP内置变量,常量,函数) PHP内置函数的性能不...

  • 2019-01-09【PHP跳转页面】

    PHP跳转页面的几种实现方法详解 •PHP页面跳转一、header()函数 header()函数是PHP中进行页面...

  • PHP面试中常遇到的问题(逐步完善答案)

    PHP相关 1、PHP基础函数使用:数组*、字符串、时间函数、数学函数、 max min array_splice...

网友评论

      本文标题:php自备函数

      本文链接:https://www.haomeiwen.com/subject/elxrsttx.html