首頁 > 後端開發 > php教程 > 收藏PHP常用函數

收藏PHP常用函數

巴扎黑
發布: 2016-11-23 15:21:18
原創
1052 人瀏覽過

最近研究PHP,收藏一下。
出處:php來源碼網-開源程式大全(Open Source) 


內容: 
 
function GetIP() { //取得IP 
if ($_SERVER["HTTP_X_FORPARD_X_FOR> "HTTP_X_FORWARDED_FOR"]; 
else if ($_SERVER["HTTP_CLIENT_IP"]) 
$ip = $_SERVER["HTTP_CLIENT_IP"]; "]; 
else if (getenv("HTTP_X_FORWARDED_FOR")) 
$ip = getenv("HTTP_X_FORWARDED_FOR"); 
else if (getenv("HTTP_CLIENT__NHT);
else if (getenv("REMOTE_ADDR")) 
$ip = getenv("REMOTE_ADDR"); 
else 
$ip = "Unknown"; 
return $ip 
} www. .jb51.net/article/22199.htm 


function DateAdd($date, $int, $unit = "d") { //時間的增加(還可以改進成時分秒都可以增加,有時間補上) 
$dateArr = explode("-", $date); 
$value[$unit] = $int; 
return date("Y-m-d", mktime(0,0,0, $dateArr[ 1] + $value['m'], $dateArr[2] + $value['d'], $dateArr[0] + $value['y'])); 

function GetWeekDay($date) { //計算出給定的日期是星期幾 
$dateArr = explode("-", $date); 
return date("w", mktime(0,0,0,$dateArr[1],$dateArr [2],$dateArr[0])); 

?> 
詳細出處參考:http://www.jb51.net/article/22199.htm 

 
function /檢查日期是否合法日期 
$dateArr = explode("-", $date); 
if (is_numeric($dateArr[0]) && is_numeric($dateArr[1]) && is_numeric($dateArr[2]) &$date) { 
return checkdate($dateArr[1],$dateArr[2],$dateArr[0]); 

return false; 

function check_time($$time = explode(":", $time); 
if (is_numeric($timeArr[0]) && is_numeric($timeArr[1]) && is_numeric($timeArr[2])) { 
if (($timeArr[0 ] >= 0 && $timeArr[0] = 0 && $timeArr[1] = 0 && $timeArr[2] return true; 
else 
return false; 

return false; 

詳細出處參考:http://www.jb51.net/artle/ate 
date1, $date2, $unit = "") { //時間比較函數,回傳兩個日期相差幾秒鐘、幾分鐘、幾小時或幾天 
switch ($unit) { 
case 's': 
$dividend = 1; 
break; 
case 'i': 
$dividend = 60; 
break; 
case 'h': 
$dividend = 3600; 
breakbreak; ; 
default: 
$dividend = 86400; 

$time1 = strtotime($date1); 
$time2 = strtotime($date2); $time2) / $dividend; 
return false; 

?> 
詳細出處參考:http://www.jb51.net/article/22199.htm 


index.php"); 
方法二:echo "window.location ="$PHP_SELF";īpt>"; 
方法三:echo ""; 
?> 
詳細來源參考:http://www.jb51.net/article/22199.htm 

PHP重定向 
複製程式碼如下: 
 
PHP重定向方法header("Location: index.php"); 
方法二:echo "window.location ="$PHP_SELF";īpt>"; 
方法三:echo ""; 
?> 

取得訪客瀏覽器 
程式碼如下: 
 
function browse_infor() 
{$ 
@@
=""; ; 
$Browsers =array("Lynx","MOSAIC","AOL","Opera","JAVA","MacWeb","WebExplorer","OmniWeb"); 
$Agent = $GLOBALS["HTTP_USER_AGENT" ]; 
for ($i=0; $i{ 
if (strpos($Agent,$Browsers[$i]))

$browser = $Browsers[$i]; 
$browserver =""; 


if (ereg("Mozilla",$Agent) && !ereg("MSIE",$Agent)) 

$temp =explode("(", $Agent); $Part=$tempe; 0]; 
$temp =explode("/", $Part); $browserver=$temp[1];
$temp =explode(" ",$browserver); preg_replace("/([d.]+) /","1",$browserver);
$browser = " $browserver";
$browser = "Netscape Navigator"

if (ereg(" Mozilla") ,$Agent) && ereg("Opera", $Agent)) 

$temp =explode("(", $Agent); $Part=$temp[1]; 
$temp =explode(") ", $part); $browserver=$temp[1 ];
$temp =explode(" ",$browserver);$browserver=$temp[2]; 
$browserver =preg_replace("/([d.] +) /","1",$browserver);
$browser = " $browserver"; 
$browser = "Opera"; 

if (ereg("Mozilla",$Agent) && ereg("MSIE" ,$ Agent)) illa",$Agent) && ereg("MSIE" ,$ Agent)) 
{temp
($
($ "(", $Agent); $Part=$temp[1]; 
$temp = 爆炸(";",$Part); $Part=$temp[ 1] ; 
$temp = 爆炸(" ",$ Part);$browserver=$temp[2];
$browserver =preg_replace("/([d.]+)/","1",$browserver) ; $browser = " $browser"; 
$browser = "Internet Explorer";

if ($browser!="")

$browseinfo = "$browser$browser"; 

else 

$browseinfo = "未知"; 

return $browseinfo; 

//呼叫方法$browser=browseinfo() ;直接回傳結果 
?> 
詳細出處參考:http://www.jb51.net/article/22199.htm 

取得存取網路程式碼如下: 
 
function osinfo() {
$os=""; 
$Agent = $GLOBALS["HTTP_USER_AGENT"]; 
if (eregi('win',$Agent) && strpos($Agent, '95')) { 
$os="Windows 95"; 

elseif (eregi('win 9x',$Agent) && strpos($Agent, '4.90')) { 
$os="Windows ME"; 

elseif (eregi('win',$Agent) && ereg('98',$Agent)) { 
$os="Windows 98"; 

elseif (eregi('win',$Agent) && eregi('nt 5.0',$Agent)) { 
$os="Windows 2000"; 

elseif (eregi('win',$Agent) && eregi('nt',$Agent)) { 
$os="Windows NT"; 

elseif (eregi('win',$Agent) && eregi('nt 5.1',​​$Agent)) { 
$os="Windows XP"; 

elseif (eregi('win',$Agent) && ereg('32',$Agent)) { 
$os="Windows 32"; 

elseif (eregi('linux',$Agent)) { 
$os="Linux"; 

elseif (eregi('unix',$Agent)) {
$os="Unix"; 

elseif (eregi('sun',$Agent) && eregi('os',$Agent)) { 
$os="SunOS"; 

elseif (eregi('ibm',$Agent) && eregi('os',$Agent)) { 
$os="IBM OS/2"; 

elseif (eregi('Mac',$Agent) && eregi('PC',$Agent)) { 
$os="Macintosh"; 

elseif (eregi('PowerPC',$Agent)) {
$os="PowerPC"; 

elseif (eregi('AIX',$Agent)) {
$os="AIX"; 

elseif (eregi('HPUX',$Agent)) { 
$os="HPUX"; 

elseif (eregi('NetBSD',$Agent)) { 
$os="NetBSD"; 

elseif (eregi('BSD',$Agent)) { 
$os="BSD"; 

elseif (ereg('OSF1',$Agent)) {
$os="OSF1"; 

elseif (ereg('IRIX',$Agent)) { 
$os="IRIX"; 

elseif (eregi('FreeBSD',$Agent)) { 
$os="FreeBSD"; 

if ($os=='') $os = "未知"; 
返回$os; 

//呼叫方法$os=os_infor() ; 
? 
$mime_types = 記憶體(
'gif'=> '圖片/gif',
'jpg' => '圖片/jpeg',
'jpeg' =>; '圖片/jpeg',
'jpe' =>; '圖片/jpeg',
'bmp' =>; '圖像/bmp',
'png' => '圖像/png',
'tif' =>; '圖片/tiff',
'tiff' => '圖片/tiff',
'pict' => '圖片/x-pict',
'pic' => '圖片/x-pict',
'pct' =>; 'image/x-pict',
'tif' => '映像/tiff',
'tiff' => '映像/tiff',
'psd' => 'image/x-photoshop',
'swf' =>; 'application/x-shockwave-flash',
'js' =>; 'application/x-javascrīpt',
'pdf' => 'application/pdf',
'ps' =>; '應用程式/postscript',
'eps' => '應用程式/postscript',
'ai' =>; '應用程式/postscrīpt',
'wmf' =>; 'application/x-msmetafile',
'css' =>; '文本/css',
'htm' =>; 'text/html',
'html' =>; 'text/html',
'txt' =>; '文字/純文字',
'xml' =>; 'text/xml',
'wml' =>; 'text/wml',
'wbmp' => 'image/vnd.wap.wbmp',
'mid' => '音頻/midi',
'wav' => '音頻/wav',
'mp3' => '音訊/mpeg',
'mp2' => '音訊/mpeg',
'avi' => '視訊/x-msvideo',
'mpeg' => '視訊/mpeg',
'mpg' => '影片/mpeg',
'qt' => '影片/quicktime',
'mov' => '影片/quicktime',
'lha' => '應用程式/x-lha',
'lzh ' =>; 'application/x-lha',
'z' =>; 'application/x-compress',
'gtar' =>; 'application/x-gtar',
'gz' =>; 'application/x-gzip',
'gzip' =>; 'application/x-gzip',
'tgz' =>; 'application/x-gzip',
'tar' =>; 'application/x-tar',
'bz2' =>; 'application/bzip2',
'zip' =>; 'application/zip',
'arj' =>; 'application/x-arj',
'rar' => 'application/x-rar-compressed',
'hqx' =>; 'application/mac-binhex40',
'sit' =>; 'application/x-stuffit',
'bin' =>; 'application/x-macbinary',
'uu' =>; 'text/x-uuencode',
'uue' =>; 'text/x-uuencode',
'latex'=> 'application/x-latex',
'ltx' =>; 'application/x-latex',
'tcl' =>; 'application/x-tcl',
'pgp' =>; 'application/pgp',
'asc' =>; 'application/pgp',
'exe' =>; 'application/x-msdownload',
'doc' => 'application/msword',
'rtf' => 'application/rtf',
'xls' =>; 'application/vnd.ms-excel',
'ppt' =>; 'application/vnd.ms-powerpoint',
'mdb' =>; 'application/x-msaccess',
'wri' =>; 'application/x-mswrite',
); 
? 
header("內容類型:application/vnd.ms-excel"); 
header("Content-Disposition:filename=test.xls"); 
回顯「test1t」;
回顯「test2tn」;
回顯「test1t」;
回顯「test2tn」;
回顯「test1t」;
回顯「test2tn」;
回顯「test1t」;
回顯「回顯「test2tn」;
回顯「test1t」;
回顯「test2tn」;
回顯「test1t」;
回顯「test2tn」;
//對應檔案頭就可以輸出.doc .xls等檔案格式了
?詳細資料參考:http://www.jb51.net/article/22199.htm 

時間比較問題 
舉一個簡單例子說明:例如論壇對當天發表的貼子用new圖片標記一下。
方法一: 
複製碼代碼如下: 
 
//$db->rows[$i][date]中為資料庫中datetime欄位值. 
$today=time(); 
$theDay=date( "Y-m-d H:i:s",$today-24*3600); 
$newTag=$db->rows[$i][date]>=$theDay?"收藏PHP常用函數":""; 
//方法二: 
$newTag=$db->rows[$i][date]>=date("Y-m-d 00:00:00")?"收藏PHP常用函數":""; 
?> 

複製程式碼如下: 
//擷取頁面和瀏覽器提交的變量,作用相當於使PHP.INI開了全域變數 
 
@extract($_SERVER, EXTR_SKIP); 
@extract($_SESSION, EXTR_SKIP); 
@extract($_POST, EXTR_SKIP); 
@extract($_FISK) ); 
@extract($_ENV, EXTR_SKIP); 
?> 

複製程式碼如下: 
//讀取檔案函數 
 
function readfromfile($file_iffromfile($file_if.
$filenum=fopen($file_name,"r"); 
flock($filenum,LOCK_EX); 
$file_data=fread($filenum, filesize($file_name)); 
rewind($filenum); filenum); 
return $file_data; 


?> 

複製程式碼如下: 
//寫入檔案函數 
$filenum=fopen($file_name,$method); 
flock($filenum,LOCK_EX); 
$file_data=fwrite($filenum,$data); 
fclose($filenum); 
return $ ?> 

複製程式碼如下: 
//頁快速轉向 
 
function turntopage($url="index.php",$info = "頁面轉向...",$second=2){ 
print "n

n頁轉向....n"; 
print "n"; 
print "n"; 
print "nn"; 
print "n"; 
print " n"; 
print " n";
print " n"; 
print " n"; 
print " n"; 
print " n"; 
print " n"; 
print " n"; 
print " n"; 
print "
n"; 
print " n"; 
print " n"; 
print " n";
print " n"; 
print " n"; 
print " n"; 
print " n"; 
print " n"; 
print " n"; 
print " n"; 
print " n"; 
print "
頁轉向提示訊息
$info
n"; 
print " 如果你的瀏覽器不支援自動跳轉,請按這裡 a>
n";
print "n"; 
exit; 
?> 

產生隨機字串函數 
複製程式碼如下: 
 
function rand#$length) {$length) {$ @#; 
$chars = @#ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz@#; 
$max = strlen($chars) - 1; 
mt_srand((double)microtime($chars) - 1; 
mt_srand((double)microtime(00); $i++ ) { 
$hash .= $chars[mt_rand(0, $max)]; 

return $hash; 

?> 

截斷長度的字串(此函數對GB2312使用有效複製碼程式碼如下: 
 
function Wordscut($string, $length ,$sss=0) { 
if(strlen($string) > $length) { 
if($sss){1000$le ; 
$addstr=@# ...@#; 

for($i = 0; $i if(ord($string[$i]) > 127) { 
$wordscut .= $string[$i].$string[$i + 1]; 
$i++; 
} else { 
$wordscut .= $string[$i]; 


} addstr; 

return $string; 

?> 

取得客戶端IP位址 
程式碼如下: 
 
function GetIP(碼CLIENT_IP "), "unknown")) 
$ip = getenv("HTTP_CLIENT_IP"); 
else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED]F "HTTP_X_FORWARDED_FOR"); 
else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) DR
$ip = getenv("REMOet_ADDR
$ @#REMOTE_ADDR@#]) && $_SERVER[@#REMOTE_ADDR@#] && strcasecmp($_SERVER[@#REMOTE_ADDR@#], "unknown")) 
$ip = $_SERVER[@#REMOTE_ADDR@#. else 
$ip = "unknown"; 
return($ip); 

?> 

判斷郵箱地址 
代碼如下: 
[a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+",$inAddress)); 

?> 

分頁(兩個函數搭配使用) 

程式碼如下: 
 
function getpage($sql,$page_size=20) 

glo. $page = $_GET["page"]; 
//$eachpage = $page_size; 
$pagesql = strstr($sql," from "); 
$pagesql = "select count(*) as ids ".$pagesql ; 
$result = mysql_query($pagesql); 
if($rs = mysql_fetch_array($result)) $sums = $rs[0]; 
$totalpage = ceil($sums/$page_size); $page)($page$startpos = ($page-1)*$page_size; 
$sql .=" limit $startpos,$page_size "; 
return $sql 
}
function showbar($string="") 

global $page,$totalpage; 
$out="共".$totalpage. "頁"; 
$linkNum =4; 
$start = ($page-round($linkNum/2))>0 ? ($page-round($linkNum/2)) : "1"; 
$end = ($page+round($linkNum/2))$prestart=$start-1; 
$nextend=$end+1; 
if($page1) 
$out .= "第一頁 "; 
if($start>1) 
$out.=".. "; 
for($t=$start;$t{ 
$out .= ($page==$t) ? "[".$t."] " : "
$t "; 

if($end$out.=">>.."; 
if($page$totalpage) 
$out .= " 最後頁"; 
return $out; 

?> 

取得新插入資料的ID 
複製程式碼
?> 
程式碼如下: 
//取得目前的腳本網址 
 
function get_php_url(){ 
if(!empty($_server["REQUEST_URI"])){ 
$scriptName = $_SERVER["REQUEST_URI"]; 
$nowurl = $scriptName. 
} ["PHP_SELF"]; 
if(empty($_SERVER["QUERY_STRING"])) $nowurl = $scriptName; 
else $nowurl = $scriptName."?".$_SERVER["QUERY_STRING"]; 
} $nowurl; 

?> 

程式碼如下: 
//將全角數字轉為半角數 
 
function GetAlabNum($fnum){ 
function GetAlabNum($fnum){($nums = array($"$" ","3","4","5","6","7","8","9"); 
$fnums = "0123456789"; 
for($i=0;$i$fnum = ereg_replace("[^0-9.]|^0{1,} ","",$fnum); 
if($fnum=="") $fnum=0; 
return $fnum; 

?> 
程式碼如下: 
//去除HTML標記標記
$txt = str_replace(" "," ",$txt); 
$txt = str_replace("$txt = str_replace(">"," >",$txt); 
$txt = preg_replace("/[rn]{1,}/isU"," 
rn",$txt); 
return $txt; 

?> 
return $txt; 

?> 

//相對路徑轉換成絕對路徑 
 
function relative_to_absolute($content, $feed_url) { 
preg_match('/(http|https|ftp):///', $feed_url, $protocol); server_url = preg_replace("/(http|https|ftp|news):///", "", $feed_url); 
$server_url = preg_replace("//.*/", "", $server_url); 
if ($server_url == '') { 
return $content; 

if (isset($protocol[0])) { 
$new_content = preg_replace('/href="//', 'href="' .$protocol[0].$server_url.'/', $content); 
$new_content = preg_replace('/src="//', 'src="'.$protocol[0].$server_url.'/ ', $new_content); 
} else { 
$new_content = $content; 

return $new_content; 

?> 

? 取得所有連結碼code){ 
preg_match_all('/"' ]+)["|']?s*[^>]*>([^>]+)/i',$code,$arr); 
return array('name'=>$arr[2],'url'=>$arr[1]); 

?> 

複製代碼程式碼如下: 
//HTML表格的每行轉為CSV格式陣列 
 
function get_tr_array($table) { 
$table = preg_replace("']*?>'si",'" ',$table); 
$table = str_replace("",'",',$table); 
$table = str_replace("","{tr}",$table) ; 
//去掉HTML 標記 
$table = preg_replace("']*?>'si","",$table); 
//去掉空白字元 
$table = preg_replace("'([rn])[s]+'","",$table); 
$table = str_replace(" ","",$table); 
$table = str_replace(" "," ",$table); 
$table = explode(",{tr}",$table); 
array_pop($table); 
return $table; 

?> 
將HTML表格的每行和每列轉為數組,收集表格資料 
 
function get_td_array($table) { 
$table = preg_replace("']*?>'si","", $table); 
$table = preg_replace("']*?>'si","",$table); 
$table = preg_replace("'","{tr}",$table); 
$table = str_replace("","{td} ",$table); 
//去掉HTML 標記 
$table = preg_replace("']*?>'si","",$table); 
//去掉空白字元 
$table = preg_replace("'([rn])[s]+'","",$table); 
$table = str_replace(" ","",$table); 
$table = str_replace(" ","",$table); 
$table = explode('{tr}', $table); 
array_pop($table); 
foreach ($table as $key=>$tr) { 
$td = explode('{td}', $tr); 
array_pop($td); 
$td_array[] = $td; 

return $td_array; 

?> 

复制代码 代码如下: 
//返回字符串中的所有单词 $distinct=true 去除重复 
function split_en_str($str,$distinct=true) {
preg_match_all('/([a-zA-Z]+)/',$str,$match);
if ($distinct == true) {
$match[1] = array_unique($match[1]);
}
sort($match[1]);
return $match[1];
}
?> 

复制代码 代码如下: 
//打印出为本PHP项目做出贡献的人员的清单 
string phpcredits(void)
?> 


//获得当前的脚本网址 
function get_php_url(){ 
        if(!empty($_SERVER["REQUEST_URI"])){ 
                $scriptName = $_SERVER["REQUEST_URI"]; 
                $nowurl = $scriptName; 
        }else{ 
                $scriptName = $_SERVER["PHP_SELF"]; 
                if(empty($_SERVER["QUERY_STRING"])) $nowurl = $scriptName; 
                else $nowurl = $scriptName."?".$_SERVER["QUERY_STRING"]; 
        } 
        return $nowurl; 




//把全角数字转为半角数字 
function GetAlabNum($fnum){ 
        $nums = array("0","1","2","3","4","5","6","7","8","9"); 
        $fnums = "0123456789"; 
        for($i=0;$i<=9;$i++) $fnum = str_replace($nums[$i],$fnums[$i],$fnum);
$fnum = ereg_replace("[^0-9\.]|^0{1,}","",$fnum);
if($fnum=="") $fnum=0;
return $fnum;
}
//去除HTML标记
function Text2Html($txt){
$txt = str_replace(" "," ",$txt);
$txt = str_replace("<","<",$txt);
$txt = str_replace(">",">",$txt); 
        $txt = preg_replace("/[\r\n]{1,}/isU","
\r\n",$txt); 
        return $txt; 



//清除HTML标记 
function ClearHtml($str){ 
        $str = str_replace('<','<',$str);
$str = str_replace('>','>',$str); 
        return $str; 

//相对路径转化成绝对路径 
function relative_to_absolute($content, $feed_url) { 
    preg_match('/(http|https|ftp):\/\//', $feed_url, $protocol); 
    $server_url = preg_replace("/(http|https|ftp|news):\/\//", "", $feed_url); 
    $server_url = preg_replace("/\/.*/", "", $server_url);  

    if ($server_url == '') { 
        return $content; 
    } 



    if (isset($protocol[0])) { 
        $new_content = preg_replace('/href="\//', 'href="'.$protocol[0].$server_url.'/', $content); 
$new_content = preg_replace('/src="//', 'src="'.$protocol[0].$server_url.'/', $new_content); 
    } else { 
   
    return $new_content; 

//取得所有連結 
function $new_content; 

//取得所有連結 
function $new_content; 

//取得所有連結 
function get_all_url($code){ 
        preg_match_all('/]*>([^>]+)/i',$code,$arr); 
        return array('name'=>$arr[2],'url' =>$arr[1]); 





//取得指定標記中的內容 
function get_tag_data($str, $start end == '' ){ 
               return; 
        } 
 str = explode($end, $str[1]); 
        return $str[0]; 

//HTML表格的每行轉為CSV格式陣列 
function get_tr_array($table) { 
        $table = preg_replace("'
",'",',$table); 
        $table= HTML 標記 




        $table = preg_replace("']*?>'si","",$ = preg_replace("'([rn])[s]+'","",$table); 
        $table = str_replace(" ","",$table); 
   ,$table);  

        $table = explode(",{tr}",$table); 
       
//將HTML表格的每行每列轉為陣列,擷取表格資料 
function get_td_array($table) { 
        $table = preg_replace("'
]*?>' si","",$table); 
$table = str_replace("
]*?>'si",'"',$ ; 
        $table = str_replace("
]* 'si","",$table); ]*?>'si","",$table); 
        $table = preg_replace("'","{tr}",$table); 
        $table = str_replace("","{td}",$   
        $table = preg_replace("']*?>'si","",$table); 
      rn])[s]+'","",$table); 




        $table = str_replace(" ","",$table); 
,$ table); 
       
        $table = explode('{tr}', $table); 
     ) { 
                $td = explode('{td }', $tr); 
array_pop($td); 
            $td_array[] = $td; 
        } 所有字$distinct=true 去除重複 
function split_en_str($str,$distinct =true) { 
        preg_match_all('/([a-zA-Z]+)/',$str,$match); 
     $match[1] = array_unique($match [1]); 
        } 
        sort($match[1]); 
        //檔案讀取函式 
   function PHP_Read($file_name) { 
   $ fd=fopen($file_name,r); 
   while($bufline=fgets($fd, 4096)){ 
   $buf.=$bufline;      } 
?> 






檔案寫入函數 




    //檔案寫入函數,$ ($file_name,$method); 
   flock($filenum,LOCK_EX); 
   $file_data=fwrite($filenum,$data); 
   fclose($filenum); 


靜態頁產生函式 


   //靜態頁產生函數 
   function phptohtm($filefrom,$fileto,$u2u=1){ 

    }else{ 
   $data=$filefrom; 
    } 
   PHP_Write($fileto,$data); 




指定條件資訊數量擷取函數 




    //指定條件資訊數量擷取函數 
   function rec_exist($table,$where){ 
   $query="select i) as num from $table ".$where; ) or die(nerror(1)); 
   $rowcount=mysql_fetch_array($result); 
   $num=$rowcount["num"]; 
   if ($num==0){ return $num; 
    } 
   ?> 





目錄刪除函式  

🠎 . dir($directory); 
   while($file =$mydir->read()){ 
   if((is_dir("$directory/$file")) AND ($file!=".") AND ($file!="..")){ 
   del_DIR ("$directory/$file"); 
    }else{ 
   if(($file!=".") AND ($file!="..")){ 
   unlink("$directory/$file") ; 
   //echo "unlink $directory/$file ok "; 
    } 
    } 
    } 

🠎);  //echo "rmdir $directory ok ";
    } 
?> 



目錄名稱合法性i   $p=ord(substr($str,$i,1)); 
   if(($p57 & $p90 & $p122){ 
   nerror("不符合規範! "); 
    } 
   $ret.=chr($p); 
    } 
   return $ret; 
  

//分頁函式 
   function splitlist($HALT,$LRLIST,$ECHOCNT,$paper,$table,$where,$page_id,$userid){ 
   global $splitstr,$sumcnt){ 
   global $splitstr,$sumcnt '; " || $sumcnt==""){ 
   $query = "從$table $where 中選擇count(*) as num"; 
   $結果= mysql_query($query); 
 ; 
   $sumcnt=$row["num"]; 
   if($sumcnt==0){ 
   nerror("版內尚未選擇發佈新聞!"); 🠎🠎150118

   $sumpaper=($sumcnt-$sumcnt%$ECHOCNT)/$ECHOCNT; 
   if(($sumcnt%$ECHOCNT)!=0) $sumpaper+=ALT; 
=0) return($where); 
   $enwhere=base64_encode(base64_encode($where)); 
   if(($LRLIST*2+1) . ){ 
   $tract=1; 
   $sub=$LRLIST*2+1; 
    }else if(($paper+$LRLIST) >= $sumpaper){ 
  
  
   $sub=$sumpaper; 
    }其他{ 
   $tract=$paper-$LRLIST; 
   $sub=$paper+$LR 
   $sub=$sumpaper; 

   $uppaper=$paper-1; 
   $downpaper=$paper+1; 
   $startcnt=($paper-1)*$ECHOCNT; 
}  $where }  $where } ; 



   if($tract > 1) { $splitstr="【    else $splitstr="【    for($i=$subtract; $i++){ 
   if ($i!=$paper) $splitstr.="".$i." "; 
   else $splitstr.="".$i." "; 
  =$sumpaper) $splitstr.=">> 】"; 
   else $splitstr.=">>】"; 
   return($where); 
    } 
   ? > 



關於分頁函數的使用說明  
      /* 
   #### 擷取分頁函數#### 
頁碼Int $LRLIST - (頁碼條顯示頁碼數-1)/2 
   Int $ECHOCNT - 搜尋時每頁顯示記錄的數量 
   Int $paper - 頁數,預先擷取:$paper=$HTTP_GET_VARS[paper]; $table - 資料表名,預先附值:$table="db.table"; 
   Varchar $where - 搜尋條件,預附值:$where="where field='value'"; 
   
   Varchar $enwhere -將原$where進行兩次base64_encode()編碼後以GET的方式提交
   Varchar $splitstr - 頁碼條輸出字符串,執行函數後在對應的位置執行echo $splitstr; 
   
   函式呼叫前需取得指標-
   $paper=$HTTP_GET_VARS[paper]; 
   $sumcnt=$HTTP_GET_VARS[sumcnt]; 
   $enwhere=$HTTP_GET_VARS[enwhere]   $enwhere=$HTTP_GET_VARS[enwhere.頁後檢索語句的檢索條件  
  注意:本函式呼叫需要錯誤處理函數nerror($error); 
   */ 




圖片檔案上傳函式 


$UploadFile_size,$UploadPath,$max_size=64){ 
   
  / /$時間限制=60; //設定逾時限制時間激勵時間為30秒設定為不限時 
($ if(($UploadFile!= "none" )&&($UploadFile != "" )){ 
   $FileName=$UploadPath.$UploadFile_name; 
   if($UploadFile_size     }elseif($UploadFile_size


   $FileSize=number_format(30
}其他{ 
nerror("檔案超過限制大小!"); 
    } 
   //{ 
   //$FileSize="number_format((double)($UploadFile_size" / (1024 * 1024), 1024), / } 
   if(!file_exists($FileName)){ 
   if(copy($UploadFile,$FileName)){ 
   return "$UploadFile_name ($FileSize)";載失敗!超時設定 
    } 





  如何判斷ip位址合法性 
if(!strcmp(long2ip(sprintf("%u",ip2long($ip))),$ip)) echo "is ipn"; 
----

email的正規判斷 
eregi("^[_.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z_-]+.)+[ a-zA-Z]$", $email); 



檢測ip位址和mask是否合法的例子 





$ip = '192.168.0.84',355% ; 
$network = '192.168.0'; 
$ip = ip2long($ip); 
$mask = ip2long($mask); 
$network = ip2long($network); mask) == $network) echo "valid ip and maskn"; 
?>  



---- 



文件下載頭部輸出如何設定




. : application/x-download"); 
header("Content-Disposition: attachment; filename=$file_download_name;"); 
header("Accept-Ranges: bytes"); 
header("Content-Lizes"); ); 
echo 'xxx' 



用header輸出ftp下載方式,並且支援斷點續傳 
一個例子: 



header('Pacheo); private'); 
header('Cache-Control: no-cache, must-revalidate'); 
header('Accept-Ranges: bytes'); 
header('ent: close'); 
header("ContConnection-Connection類型: audio/mpeg"); 
header("Location:ftp://download:1bk3l4s3k9s2@232.2.22.22/2222/web技術開發知識庫/cn_web.rmvb");中文 
ereg("^[".chr(0xa1)."-".chr(0xff)."]+$", $str); 

批量替換文本裡面的超級連結 




? php 
function urlParse($str = ''){ 
    if ('' == $str) return $str; 
$types = array("http", "ftp", "https");   ''.htmlentities('1').htmlentities('2').'
EOPHP; 




$ret = $str; 

while(list(,$type) = each($types)){ 
_retype [^s]*)|ie ", $replace, $ret); 




return $ret; 

?>
]*?>'si","",$table); 
   table = str_replace("
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板