成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

PHP實(shí)例方法有什么-創(chuàng)新互聯(lián)

這篇文章主要介紹PHP實(shí)例方法有什么,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

成都創(chuàng)新互聯(lián)于2013年創(chuàng)立,先為虎丘等服務(wù)建站,虎丘等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為虎丘企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

PHP實(shí)例方法的例子有:1、PHPExcel讀取Excel,2、獲取文本中首張圖片地址;3、將圖片保存到本地;4、返回JSON數(shù)據(jù);5、【var_dump】函數(shù)改寫;6、圖片轉(zhuǎn)為base64格式等等。

PHP實(shí)例方法的例子有:

  • PHPExcel 讀取Excel

  • 獲取文本中首張圖片地址

  • 將圖片保存到本地

  • 返回JSON數(shù)據(jù)

  • var_dump 函數(shù)改寫

  • 圖片轉(zhuǎn)為base64格式

  • 使用curl 實(shí)現(xiàn)get請(qǐng)求

  • 使用curl 實(shí)現(xiàn)post請(qǐng)求

  • 簡單的xml轉(zhuǎn)數(shù)組方法

  • Utf-8轉(zhuǎn)統(tǒng)一碼

  • 字符串轉(zhuǎn)統(tǒng)一編碼

  • 獲取IP地址

  • 創(chuàng)建隨機(jī)字符串

  • 根據(jù)生日獲取年齡

  • 根據(jù)經(jīng)緯度計(jì)算距離

PHPExcel 讀取excel

function readExcel($filename, $encode = 'utf-8')
{
//    import("ORG.Util.PHPExcel.IOFactory");
    import("Org/Util/PHPExcel");
    if (strpos($filename, "xlsx")) {
        $objReader = PHPExcel_IOFactory::createReader('Excel2007');
    } else {
        $objReader = PHPExcel_IOFactory::createReader('Excel5');
    }
    $objReader->setReadDataOnly(true);
    $objPHPExcel = $objReader->load($filename);
    $objWorksheet = $objPHPExcel->getActiveSheet();
    $highestRow = $objWorksheet->getHighestRow();
    $highestColumn = $objWorksheet->getHighestColumn();
    $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
    $excelData = array();
    for ($row = 1; $row <= $highestRow; $row++) {
        if ((string)$objWorksheet->getCellByColumnAndRow(0, $row)->getValue() == "") continue;
        for ($col = 0; $col < $highestColumnIndex; $col++) {
            $value = (string)$objWorksheet->getCellByColumnAndRow($col, 1)->getValue();
            if ($value == "") {
                continue;
            }
            $excelData[$row - 1][] = (string)$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
        }
    }
    return $excelData;
}

獲取文本中首張圖片地址

function getFirstPic($content){
    if(preg_match_all("/(src)=([\"|']?)([^ \"'>]+\.(gif|jpg|jpeg|bmp|png))\\2/i", $content, $matches)){
        $str=$matches[3][0];
        if(preg_match('/\/ueditor\/php\/upload\/image/',$str)){
            return $str1=substr($str,6);
        }
    }
}

將圖片保存到本地

function getImage($url,$save_dir='',$filename='',$type=1){
    if(trim($url)==''){
        return array('file_name'=>'','save_path'=>'','error'=>1);
    }
    if(trim($save_dir)==''){
        $save_dir='./';
    }
    if(trim($filename)==''){//保存文件名
        $ext = strrchr($url,'.');
        if($ext!='.gif'&&$ext!='.jpg'){
            return array('file_name'=>'','save_path'=>'','error'=>3);
        }
        $filename=time().$ext;
    }
    if(0!==strrpos($save_dir,'/')){
        $save_dir.='/';
    }
    //創(chuàng)建保存目錄
    if(!file_exists($save_dir)&&!mkdir($save_dir,0777,true)){
        return array('file_name'=>'','save_path'=>'','error'=>5);
    }
    //獲取遠(yuǎn)程文件所采用的方法
    if($type){
        $ch=curl_init();
        $timeout=5;
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
        $img=curl_exec($ch);
        curl_close($ch);
    }
    else{
        ob_start();
        readfile($url);
        $img=ob_get_contents();
        ob_end_clean();
    }
    $size=strlen($img);
    echo $size;
    //文件大小
    $fp2=fopen($save_dir.$filename,'a');
    fwrite($fp2,$img);
    fclose($fp2);
    unset($img,$url);
    return array('file_name'=>$filename,'save_path'=>$save_dir.$filename,'error'=>0);
}

返回JSON數(shù)據(jù)

function show($status, $msg, $closeCurrent=false, $data=array()){
    $tmpArr = array(
        'statusCode' => $status,
        'message'    => $msg,
        'closeCurrent' => $closeCurrent,
    );
    $tmpArr = array_merge($tmpArr, $data);
    exit(json_encode($tmpArr));
}

var_dump 函數(shù)改寫

function lyl_dump($content){
    header("Content-type:text/html;charset=utf-8");
    echo '<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />';
    echo "<pre>";
    var_dump($content);
    echo "<pre/>";
    die;
}

圖片轉(zhuǎn)為base64格式

function base64EncodeImage ($image_file) {
    if(!file_exists($image_file)){
        return false;
    }
    $image_info = getimagesize($image_file);
    $image_data = fread(fopen($image_file, 'r'), filesize($image_file));
    $base64_image = chunk_split(base64_encode($image_data));
    return $base64_image;
}

使用curl 實(shí)現(xiàn)get請(qǐng)求

function httpGet($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); //這個(gè)是的ssl校驗(yàn),需要驗(yàn)證
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true); //
    curl_setopt($curl, CURLOPT_URL, $url);
    $res = curl_exec($curl);
    curl_close($curl);
    return $res;
}

使用curl 實(shí)現(xiàn)post 請(qǐng)求

function httpPost($url,$post_data){
    $curl = curl_init();
    $post_data = json_encode($post_data);
    curl_setopt($ch , CURLOPT_URL , $url);
    curl_setopt($ch , CURLOPT_HEADER , 0 );
    curl_setopt( $ch, CURLOPT_POST, 1);          //設(shè)置為POST方式
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch , CURLOPT_POSTFIELDS , $post_data);
    $rst = curl_exec( $ch );
    curl_close( $ch );
    return $rst;
}

簡單的xml轉(zhuǎn)數(shù)組方法

function simplexml_to_array($simplexml_obj, $array_tags = array(), $strip_white = 1)
{
    if ($simplexml_obj) {
        if (count($simplexml_obj) == 0)
            return $strip_white ? trim((string)$simplexml_obj) : (string)$simplexml_obj;
        $attr = array();
        foreach ($simplexml_obj as $k => $val) {
            if (!empty($array_tags) && in_array($k, $array_tags)) {
                $attr[] = simplexml_to_array($val, $array_tags, $strip_white);
            } else {
                $attr[$k] = simplexml_to_array($val, $array_tags, $strip_white);
            }
        }
        return $attr;
    }
    return FALSE;
}

Utf-8轉(zhuǎn)統(tǒng)一碼

function utf8_to_unicode($char)
{
    switch (strlen($char)) {
        case 1:
            return ord($char);
        case 2:
            $n = (ord($char[0]) & 0x3f) << 6;
            $n += ord($char[1]) & 0x3f;
            return $n;
        case 3:
            $n = (ord($char[0]) & 0x1f) << 12;
            $n += (ord($char[1]) & 0x3f) << 6;
            $n += ord($char[2]) & 0x3f;
            return $n;
        case 4:
            $n = (ord($char[0]) & 0x0f) << 18;
            $n += (ord($char[1]) & 0x3f) << 12;
            $n += (ord($char[2]) & 0x3f) << 6;
            $n += ord($char[3]) & 0x3f;
            return $n;
    }
}

字符串轉(zhuǎn)統(tǒng)一編碼

function str_to_unicode_word($str,$depart=' ')
{
    $arr = array();
    $str_len = mb_strlen($str,'utf-8');
    for($i = 0;$i < $str_len;$i++)
    {
        $s = mb_substr($str,$i,1,'utf-8');
        if($s != ' ' && $s != ' ')
        {
            $arr[] = 'ux'.utf8_to_unicode($s);
        }
    }
    return implode($depart,$arr);
}

獲取IP地址

function getIP()
{
    static $realip;
    if (isset($_SERVER)) {
        if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
            $realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
        } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
            $realip = $_SERVER["HTTP_CLIENT_IP"];
        } else {
            $realip = $_SERVER["REMOTE_ADDR"];
        }
    } else {
        if (getenv("HTTP_X_FORWARDED_FOR")) {
            $realip = getenv("HTTP_X_FORWARDED_FOR");
        } else if (getenv("HTTP_CLIENT_IP")) {
            $realip = getenv("HTTP_CLIENT_IP");
        } else {
            $realip = getenv("REMOTE_ADDR");
        }
    }
    return $realip;
}

創(chuàng)建隨機(jī)字符串

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;
}

根據(jù)生日獲取年齡

function get_age($birthday){
    if($birthday){
        list($y1,$m1,$d1) = explode("-",date("Y-m-d",$birthday));
        list($y2,$m2,$d2) = explode("-",date("Y-m-d",time()));
        $age = $y2-$y1;
        if(intval($m2.$d2) < intval($m1.$d1)) {$age -= 1;}
        return $age;
    }else{
        return "未知";
    }
}

根據(jù)經(jīng)緯度計(jì)算距離

function getDistance($lat1, $lng1, $lat2, $lng2)
{
    $earthRadius = 6367000; 
    $lat1 = ($lat1 * pi() ) / 180;
    $lng1 = ($lng1 * pi() ) / 180;
    $lat2 = ($lat2 * pi() ) / 180;
    $lng2 = ($lng2 * pi() ) / 180;
    $calcLongitude = $lng2 - $lng1;
    $calcLatitude = $lat2 - $lat1;
    $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
    $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
    $calculatedDistance = $earthRadius * $stepTwo;
    return round($calculatedDistance);
}

以上是PHP實(shí)例方法有什么的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道!

當(dāng)前題目:PHP實(shí)例方法有什么-創(chuàng)新互聯(lián)
文章轉(zhuǎn)載:http://jinyejixie.com/article8/hggip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、手機(jī)網(wǎng)站建設(shè)、做網(wǎng)站、ChatGPT、網(wǎng)站設(shè)計(jì)公司服務(wù)器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化
阳朔县| 太仆寺旗| 顺昌县| 隆德县| 宝山区| 锡林浩特市| 贵德县| 启东市| 上蔡县| 凌云县| 梅州市| 新兴县| 广昌县| 乾安县| 吴堡县| 米林县| 宁远县| 长葛市| 太和县| 中西区| 仁布县| 吉水县| 兰州市| 随州市| 宁蒗| 峡江县| 陕西省| 涡阳县| 会东县| 屏边| 丰城市| 慈利县| 南木林县| 鱼台县| 江油市| 河曲县| 怀远县| 临沭县| 乌兰察布市| 连城县| 依兰县|