php工具方法

备忘常用方法  

1.寻找子栏目(权限\菜单列表。。。)


  function getSon($list,$pid){


    $arr='';
foreach ($list as $k=>$v){
if($v['pid']==$pid){
$v['son']=getSon($list,$v['id']);
$arr[]=$v;

}

}
return
$arr;
}


2.删除文件
private function _deleteDir($R){
        //打开一个目录句柄
        $handle = opendir($R);
        //读取目录,直到没有目录为止
        while(($item = readdir($handle)) !== false){
            //跳过. ..两个特殊目录
            if($item != '.' and $item != '..'){
                //如果遍历到的是目录
                if(is_dir($R.'/'.$item)){
                    //继续向目录里面遍历
                    $this->_deleteDir($R.'/'.$item);
                }else{
                    //如果不是目录,删除该文件
                    if(!unlink($R.'/'.$item))
                        die('error!');
                }
            }
        }
        //关闭目录
        closedir( $handle );
        //删除空的目录
        return rmdir($R);
    }

 3.正则匹配百度首页logo地址

//获取百度logo图片地址
$url="https://www.baidu.com";
$f=file_get_contents($url);
preg_match_all("<img.*?src=\"(.*?.*?(logo).*?)\".*?>",$f,$match);
foreach($match[1] as $val){
    echo $val;
    echo "</br>";
}

 3.1   常见邮箱验证规则:^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$


4.图片上传

function saveAvatar($content) {
        global $_W;
        if (empty($content)) {
            $this->exitJson("图片未找到", 1);
        }
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $content, $result)) {
            $type = $result[2];
            $img = base64_decode(str_replace($result[1], '', $content));
        } else {
            $img = base64_decode($content);
            $type = "jpg";
        }
        $fileUrl = "attachment/images/" . $_W['uniacid'] . "/" . date("Y") . "/" . date("m") . "/";
        if(!is_dir(IA_ROOT ."/".$fileUrl)){
            mkdir(IA_ROOT ."/".$fileUrl,0777);
        }
        $imageName = random(15) . time() . "." . $type;
        $size = file_put_contents(IA_ROOT . "/" . $fileUrl . $imageName, $img);
        if ($size > 2 * 1024 * 1024) {
            $this->exitJson("图片过大!", 1);
        }
        return $_W['siteroot'] . $fileUrl . $imageName;
    }

5.查找下级id(递归)
function getSubs($id, $includeSelf = true){
    static $ids = [];
    if($includeSelf) {
        if(!in_array($id, $ids)) {
            array_push($ids, $id);
        }
    }
    $subIds = M('user')
        ->field('user_id')
        ->select();
    $subIds = array_column($subIds, 'user_id');
    $ids = array_unique(array_merge($ids, $subIds));
    foreach($subIds as $sub_id) {
        getSubs($sub_id, $includeSelf);
    }

    return $ids;
}

5.2 递归查找下级(循环)

function getSubIds($userID, $includeSelf = true){
    $userIDs = [$userID];
    while (true) {
        $subIDs = M('user')
            ->field('user_id')
            ->where(['pid' => ['IN', $userIDs]])
            ->select();
        $subIDs = array_column($subIDs, 'user_id');
        $userCount = count($userIDs);
        $userIDs = array_unique(array_merge($userIDs, $subIDs));
        if ($userCount == count($userIDs)) {
            break;
        }
    }
    if (!$includeSelf) {
        for ($i = 0; $i < count($userIDs); ++$i) {
            if ($userIDs[$i] == $userID) {
                array_splice($userIDs, $i, 1);
                break;
            }
        }
    }

    return $userIDs;
}

 6. php 命令行下执行定时任务:php index.php /crontab/index/cron

  

 7. 判断是不是同一条线的用户

/**
 * 检测是不是同一条线
 * @param     $uid
 * @param     $pid
 * @param int $depth
 * @return bool
 */
function checkIsBelong( $uid , $pid , $depth = 0 )
{

    if ( $uid == $pid && $depth > 0 ) {
        return true;
    }

    $uid = query_user ( $uid , 'ue_accname' );
    if ( !empty($uid) ) {
        return checkIsBelong ( $uid , $pid , $depth + 1 );
    }

    return false;
}

//获取用户信息
function query_user( $uid , $field = false )
{
    $model = M ( 'user' );
    if ( $field ) {
        $arr_info = $model->field ( $field )->where ( [ 'UE_account' => $uid ] )->find ();
        return $arr_info[ $field ];
    } else {
        $arr_info = $model->where ( [ 'UE_account' => $uid ] )->find ();
        return $arr_info;
    }
}

 

 


 


posted @ 2017-09-06 13:39  spritphp  阅读(218)  评论(0编辑  收藏  举报