首页 > 开发 > PHP > 正文

PHP常用技巧汇总

2024-05-04 22:01:46
字体:大 中 小
来源:转载
供稿:网友

本文为大家分享了多个php常用技巧,供大家参考,具体内容如下

1、PHP文件读取函式

  1. //文件读取函式 
  2.  
  3. function PHP_Read($file_name) { 
  4.  
  5. $fd=fopen($file_name,r); 
  6.  
  7. while($bufline=fgets($fd, 4096)){ 
  8.  
  9. $buf.=$bufline; 
  10.  
  11. } 
  12.  
  13. fclose($fd); 
  14.  
  15. return $buf; 
  16.  
  17. } 

2、文件写入函式

  1. //文件写入函式 
  2.  
  3. function PHP_Write($file_name,$data,$method=”w”) { 
  4.  
  5. $filenum=@fopen($file_name,$method); 
  6.  
  7. flock($filenum,LOCK_EX); 
  8.  
  9. $file_data=fwrite($filenum,$data); 
  10.  
  11. fclose($filenum); 
  12.  
  13. return $file_data; 
  14.  
  15. } 

3、静态页面生成函式

  1. //静态页面生成函式 
  2.  
  3. function phptohtm($filefrom,$fileto,$u2u=1){ 
  4.  
  5. if($u2u==1){ 
  6.  
  7. $data=PHP_Read($filefrom); 
  8.  
  9. }else{ 
  10.  
  11. $data=$filefrom; 
  12. //Vevb.com 
  13. } 
  14.  
  15. PHP_Write($fileto,$data); 
  16.  
  17. return true; 
  18.  
  19. } 

4、指定条件信息数量检索函式

  1. //指定条件信息数量检索函式 
  2.  
  3. function rec_exist($table,$where){ 
  4.  
  5. $query=”select count(*) as num from $table “.$where; 
  6.  
  7. $result=mysql_query($query) or die(nerror(1)); 
  8.  
  9. $rowcount=mysql_fetch_array($result); 
  10.  
  11. $num=$rowcount["num"]; 
  12.  
  13. if ($num==0){ 
  14.  
  15. return false; 
  16.  
  17. } 
  18.  
  19. return $num; 
  20.  
  21. } 

5、目录删除函式

  1. //目录删除函式 
  2.  
  3. function del_DIR($directory){ 
  4.  
  5. $mydir=dir($directory); 
  6.  
  7. while($file=$mydir->read()){ 
  8.  
  9. if((is_dir(“$directory/$file”)) AND ($file!=”.”) AND ($file!=”..”)){ 
  10.  
  11. del_DIR(“$directory/$file”); 
  12.  
  13. }else{ 
  14.  
  15. if(($file!=”.”) AND ($file!=”..”)){ 
  16.  
  17. unlink(“$directory/$file”); 
  18.  
  19. //echo “unlink $directory/$file ok “; 
  20.  
  21. } 
  22.  
  23. } 
  24.  
  25. } 
  26.  
  27. $mydir->close(); 
  28.  
  29. rmdir($directory); 
  30.  
  31. //echo “rmdir $directory ok “; 
  32.  
  33. } 

6、目录名称合法性检测函式

  1. //目录名称合法性检测 
  2.  
  3. function isen($str){ 
  4.  
  5. $ret=”"; 
  6.  
  7. for($i=0;$i $p=ord(substr($str,$i,1)); 
  8.  
  9. if(($p<48 & $p!=45 & $p!=46) || ($p>57 & $p<65) || ($p>90 & $p<97 & $p!=95) || $p>122){ 
  10.  
  11. nerror(“不符合规范!”); 
  12.  
  13. } 
  14.  
  15. $ret.=chr($p); 
  16.  
  17. } 
  18.  
  19. return $ret; 
  20.  
  21. } 

7、PHP分页函数

  1. //分页函数 
  2.  
  3. function splitlist($HALT,$LRLIST,$ECHOCNT,$paper,$table,$where,$page_id,$userid){ 
  4.  
  5. global $splitstr,$sumcnt; 
  6.  
  7. if($paper==”" || $sumcnt==”"){ 
  8.  
  9. $query = “select count(*) as num from $table $where”; 
  10.  
  11. $result = mysql_query($query); 
  12.  
  13. $row = mysql_fetch_array($result); 
  14.  
  15. $sumcnt=$row["num"]; 
  16.  
  17. if($sumcnt==0){ 
  18.  
  19. nerror(“该版内还没有选择发布新闻 !”); 
  20.  
  21. } 
  22.  
  23. $paper=1; 
  24.  
  25. } 
  26.  
  27. $sumpaper=($sumcnt-$sumcnt%$ECHOCNT)/$ECHOCNT; 
  28.  
  29. if(($sumcnt%$ECHOCNT)!=0) $sumpaper+=1; 
  30.  
  31. if($sumpaper==1 && $HALT==0) return($where); 
  32.  
  33. $enwhere=base64_encode(base64_encode($where)); 
  34.  
  35. if(($LRLIST*2+1) < $sumpaper){ 
  36.  
  37. if(($paper-$LRLIST) < 2){ 
  38.  
  39. $tract=1; 
  40.  
  41. $sub=$LRLIST*2+1; 
  42.  
  43. }else if(($paper+$LRLIST) >= $sumpaper){ 
  44.  
  45. $tract=$sumpaper-($LRLIST*2); 
  46.  
  47. $sub=$sumpaper; 
  48.  
  49. }else{ 
  50.  
  51. $tract=$paper-$LRLIST; 
  52.  
  53. $sub=$paper+$LRLIST; 
  54.  
  55. } 
  56.  
  57. }else{ 
  58.  
  59. $tract=1; 
  60.  
  61. $sub=$sumpaper; 
  62.  
  63. } 
  64.  
  65. $uppaper=$paper-1; 
  66.  
  67. $downpaper=$paper+1; 
  68.  
  69. $startcnt=($paper-1)*$ECHOCNT; 
  70.  
  71. $where.=” limit ${ startcnt },${ ECHOCNT }”; 
  72.  
  73. if($tract > 1) { $splitstr=”【 << “; } 
  74.  
  75. else $splitstr=”【 << “; 
  76.  
  77. for($i=$tract;$i<=$sub;$i++){ 
  78.  
  79. if ($i!=$paper) $splitstr.=”".$i.” “; 
  80.  
  81. else $splitstr.=”".$i.” “; 
  82.  
  83. } 
  84. //Vevb.com 
  85. if ($sub!=$sumpaper) $splitstr.=”>> 】”; 
  86.  
  87. else $splitstr.=”>> 】”; 
  88.  
  89. return($where); 
  90.  
  91. } 
  92.  
  93. ?> 

PHP关于分页函式的使用说明

8、PHP图片文件上传函式

  1. //图片文件上传函式 
  2.  
  3. function upload_img($UploadFile,$UploadFile_name,$UploadFile_size,$UploadPath,$max_size=64){ 
  4.  
  5. //$TimeLimit=60; //设置超时限制时间 缺省时间为 30秒 设置为0时为不限时 
  6.  
  7. //set_time_limit($TimeLimit); 
  8.  
  9. if(($UploadFile!= “none” )&&($UploadFile != “” )){ 
  10.  
  11. $FileName=$UploadPath.$UploadFile_name; 
  12.  
  13. if($UploadFile_size <1024){ 
  14.  
  15. $FileSize=”(string)$UploadFile_size” . “字节”; 
  16.  
  17. }elseif($UploadFile_size <(1024 * $max_size)){ 
  18.  
  19. $FileSize=number_format((double)($UploadFile_size / 1024), 1) . ” KB”; 
  20.  
  21. }else{ 
  22.  
  23. nerror(“文件超过限制大小!”); 
  24.  
  25. } 
  26.  
  27. //{ 
  28.  
  29. //$FileSize=”number_format((double)($UploadFile_size” / (1024 * 1024)), 1) . ” MB”; 
  30.  
  31. // } 
  32.  
  33. if(!file_exists($FileName)){ 
  34.  
  35. if(copy($UploadFile,$FileName)){ 
  36.  
  37. return “$UploadFile_name ($FileSize)”; 
  38.  
  39. }else{ 
  40.  
  41. nerror(“文件 $UploadFile_name 上载失败!”); 
  42.  
  43. } 
  44.  
  45. unlink($UploadFile); 
  46.  
  47. }else{ 
  48.  
  49. nerror(“文件 $UploadFile_name 已经存在!”); 
  50.  
  51. } 
  52.  
  53. //set_time_limit(30); //恢复缺省超时设置 
  54.  
  55. } 
  56.  
  57. } 

以下是一些小技巧:

1、 PHP如何判断ip地址合法性

  1. 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是否合法的例子

  1. $ip = '192.168.0.84′; 
  2.  
  3. $mask = '255.255.255.0′; 
  4.  
  5. $network = '192.168.0′; 
  6.  
  7. $ip = ip2long($ip); 
  8.  
  9. $mask = ip2long($mask); 
  10.  
  11. $network = ip2long($network); 
  12.  
  13. if( ($ip & $mask) == $network) echo “valid ip and maskn”; 

2、PHP文件下载头部输出如何设定

  1. header(“Content-type: application/x-download”); 
  2.  
  3. header(“Content-Disposition: attachment; filename=$file_download_name;”); 
  4.  
  5. header(“Accept-Ranges: bytes”); 
  6.  
  7. header(“Content-Length: $download_size”); 
  8.  
  9. echo ‘xxx' 

3、PHP用header输出ftp下载方式,并且支持断点续传

一个例子:

  1. header(‘Pragma: public'); 
  2.  
  3. header(‘Cache-Control: private'); 
  4.  
  5. header(‘Cache-Control: no-cache, must-revalidate'); 
  6.  
  7. header(‘Accept-Ranges: bytes'); 
  8.  
  9. header(‘Connection: close');//Vevb.com 
  10.  
  11. header(“Content-Type: audio/mpeg”); 
  12.  
  13. header(“Location:ftp://download:1bk3l4s3k9s2@232.2.22.22/2222/web技术开发知识库/cn_web.rmvb”); 

4、PHP正则匹配中文

ereg(“^[".chr(0xa1)."-".chr(0xff)."]+$”, $str);

5、批量替换文本里面的超级链接

  1. function urlParse($str = ”){ 
  2.  
  3. if (” == $str) return $str; 
  4.  
  5. $types = array(“http”, “ftp”, “https”); 
  6.  
  7. $replace = << 
  8.  
  9. ”.htmlentities('1′).htmlentities('2′).” 
  10.  
  11. EOPHP; 
  12.  
  13. $ret = $str; 
  14.  
  15. while(list(,$type) = each($types)){ 
  16. //Vevb.com 
  17. $ret = preg_replace(“|($type://)([^s]*)|ie “, $replace, $ret); 
  18.  
  19. } 
  20.  
  21. return $ret; 
  22.  
  23. } 

以上就是本文的全部内容,很常用的php技巧送给大家,希望大家喜欢。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表