新增PHP經典筆記


【設置編碼】php

header("Content-type: text/html; charset=utf-8");html

 

1. parse_url — 解析 URL,返回其組成部分mysql

$ua=parse_url("http://username:password@hostname/path?arg=value#anchor");web

Array ( [scheme] => http [host] => hostname [user] => username [pass] => password [path] => /path [query] => arg=value [fragment] => anchor )sql

 

2. PHP輸出CSVEXCEL兩種簡單的方法:shell

header("Content-type:application/vnd.ms-excel");數據庫

header("Content-Disposition:filename=php100.xls");數組

 

3.array_count_values() 函數用於統計數組中全部值出現的次數。服務器

 

4.reset() 函數把數組的內部指針指向第一個元素,並返回這個元素的值。若失敗,則返回 FALSE。網絡

 

4.is_writable() 檢查文件是否可寫,用來判斷文件權限,同類函數還有is_readable() 檢查文件是否可讀,返回布爾值

 

5.PHP中三個調用系統命令的函數system(), exec(), passthru() 比較:

    system() 輸出並返回最後一行shell結果。
    exec() 不輸出結果,返回最後一行shell結果
    passthru() 只調用命令,把運行結果原樣地輸出
    new com()  系統預約義com類,根據須要任意選擇內置方法

    相同點:均可以得到命令執行的狀態碼

 

6.函數的使用說明:

    ①常規函數 

       bool copy ( string source, string dest )

    ②帶有mixed , mixed表示能夠傳任何類型的數據

       bool chown ( string filename, mixed user )

    ③帶有&參數的函數,表示引用賦值,這個參數不能傳值,只能傳一個變量,而後函數將變量的值改變,咱們在使用這個變量時,值也是變化的

        bool arsort ( array &array [, int sort_flags] )

    ④默認函數帶有[]的函數, 表示這個參數是可選的,若是你傳值了就使用你傳的值,若是沒有傳值則使用默認值。直接在聲明函數時,就爲參數給初值。可選值和必須值,必須從後向前設置。

        bool arsort ( array &array [, int sort_flags] )

    ⑤帶有...的參數函數, ...表示能夠傳任意多個參數

        int array_unshift ( array &array, mixed var [, mixed ...] )

    ⑥回調函數帶有callback,  就是調用這個函數時須要咱們傳一個函數進來(函數名,函數名字串)

        array array_filter ( array input [, callback callback] )

 

7. 預約義數組:自動全局變量---超全局數組

    $_GET       //經由URL請求提交至腳本的變量

    $_POST      //經由HTTP POST 方法提交到腳本的變量

    $_REQUEST   //經由GET, POST和COOKIE機制提交到腳本的變量,所以該數組並不值得信任,不去使用(儘可能)

    $_FILES     //經由HTTP POST  文件上傳而提交至腳本,文件處理一章,文件上傳

    $_COOKIE    //

    $_SESSION   //

    $_ENV       //執行環境提交至腳本的變量

    $_SERVER    //變量由WEB服務器設定的,或直接和當前腳本的執行環境相關聯

    $GLOBALS    //只要是當前腳本有效的變量都在這裏,數組鍵名爲全局變量的名稱

 

8.pow(x,y) 函數返回 x 的 y 次方。echo pow(6,2); //36

 

9.路徑分隔符號DIRECTORY_SEPARATOR  爲不一樣平臺,在Windows \ Linux /

 

10.PHP中遍歷stdclass object:$array = get_object_vars($test);  轉換爲數組後遍歷

 

 

《多表查詢》

$table="qg_ws_zu a,qg_ws_ac b";

$field="a.id,a.total_money,a.hongbao_num";

$order="a.order_sort desc,b.last_create_time_gid desc";

$map['a.is_del']=0;

$limit=(($_GET['page']-1)*$_GET['limit']).','.$_GET['limit'];

$list = M('')->table($table)->field($field)->where($map)->order($order)->limit($limit)->select();

$sql_res = M('')->getLastSql();

 


1.CSS控制頁面文字不能複製:body{-webkit-user-select:none;}
1.PHP中遍歷stdclass object:$array = get_object_vars($test);  轉換爲數組後遍歷
2. CSS控制頁面文字不能複製:body{-webkit-user-select:none;}
 

2.【php過濾漢字和非漢字】<?php $sc="aaad....##--__i漢字過濾"; //iconv("UTF-8","GB2312",$sc);utf-8轉碼 echo $temp=eregi_replace("[^\x80-\xff]","",$sc); //保留漢字(過濾非漢字) echo $temp=preg_replace("/[\\x80-\\xff]/","",$sc); //保留非漢字(過濾漢字),注意兩條反斜線 ?>

3.查詢數據庫中某個字段中用逗號分隔的字符串是否包含用戶傳遞的某個值,這樣寫SQL語句:SELECT * FROM tb WHERE field LIKE '%,1' OR field LIKE '1,%' OR field LIKE '%,1,%' OR field = '1' 查詢結果見圖。不知道是否有更簡單的方法,目前這樣可行。感謝學敏!


4.PHP上傳文件獲取後綴名:$temp_arr = explode(".", $upload_file_name); $file_ext = array_pop($temp_arr); $file_ext = trim($file_ext); $file_ext = strtolower($file_ext); //$file_ext 文件的後綴


5.PHP獲取圖片尺寸大小< ?php $arr=getimagesize("1.jpg"); echo $arr[0]."< br/ >"; //the width echo $arr[1]; //the heigth? >


6.PHP:Deprecated: Function set_magic_quotes_runtime() is deprecated解決方案:php.ini 修改:error_reporting = E_ALL & ~E_NOTICE & ~E_DEPRECATED


7.echo $thisUrl=dirname('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']).'/../../';exit;


8.PHP 匹配多對中括號中的內容,以下:$string = "這是[3]def[25]個人[26]想說的話[wer3][as][41]正則匹配!";preg_match_all('/\[([a-z0-9]+)\]/', $string, $matches);print_r($matches);


9.$qqContent='(頁面內容)'; $pat = '/<div class="allnum">.+?<\/div>/'; $str=str_replace("\r", "",$qqContent);$str=str_replace("\n", "",$str); preg_match_all($pat, $str, $res); 提取數字用: preg_match_all('/\d+/', $res[0][0], $nums); print_r($nums[0][0]);獲得結果。


10.PHP獲取當前頁面的全部超連接: $str = '(包含超連接的字符串)'; $pat = '/<a(.*?)href="(.*?)"(.*?)>(.*?)<\/a>/i'; preg_match_all($pat, $str, $m); print_r($m); 其中$m[2]爲超連接內容,$m[4]爲超連接的標題。


11.Linux 解壓tar.bz2方法:tar -vxjf filename.tar.bz2 解壓tar.gz 方法: tar zxvf filename.tar.gz 解壓zip方法:unzip filename.zip


12.Linux 壓縮當前目錄下的文件夾:tar -zcvf common.tar.gz common,此命令:壓縮當前目錄下的common文件夾


13.匹配圖片路徑:若是源代碼中有空格,先去掉「\r」和"\n",而後再匹配。
$str=str_replace("\r", "",$content); $str=str_replace("\n", "",$str); $pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/"; preg_match_all($pattern,$str,$match); print_r($match);


14.PHP中在遠程路徑(FTP)進行操做,好比建立文件夾:$remote="ftp://username:password@www.baidu.com/test/txt";mkdir($remote, 0777, true ); 意思即爲:在baidu網站根目錄建立目錄 /test/txt...,其中username和password分別爲當前網站的FTP登陸帳號和密碼。


15.判斷一個網絡文件是否存在(兼容全部可能性):function checkRemoteFileExists($file) {return (bool)fopen($file, 'rb');}


16.PHP判斷一個遠程文件是否存在:$url = "http://url.cn/ERUKm9 ";$fileExists = @file_get_contents($url, null, null, -1, 1) ? true : false;echo $fileExists; //返回1,就說明文件存在。


17.PHP匹配圖片路徑:preg_match_all("/<img.*src\s*=\s*[\"|\']?\s*([^>\"\'\s]*)/i",str_ireplace("\\","",$content,$arrs));$arrs[1][0]就是圖片的路徑!


18.$ch = curl_init();curl_setopt($ch, CURLOPT_URL,$userinfo);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1) ; $string= curl_exec($ch); //file_put_contents('string.txt', $string);


19.PHP中讀取https請求協議的網頁內容,有時候file_get_contents()會出錯,好比這樣的:$userinfo ='https://...';可使用以下方法:(見下條內容)


20.PHP正則匹配一對中括號內的內容爲空:$ss="sdfghjfsd[url]88888888888888[/url]"; $ss = preg_replace('/\[url\S*\[\/url\]/', '', $ss); echo $ss;


21.MySQL配置日誌文件:在MySQL安裝目錄下my.ini裏面找到[mysqld],給裏面加入log="E:/mysql_log.txt",而後重啓Apache,重啓MySQL服務便可。

22.mysql獲取上個月的當前時間:select date_sub(now(),interval 1 month) ;獲取上個月的第一天:select date_sub(date_sub(date_format(now(),'%y-%m-%d 08:00:00'),interval extract( day from now())-1 day),interval 1 month);

相關文章
相關標籤/搜索