首页 > 开发 > PHP > 正文

php开发中常用的技巧分享

2024-05-04 21:58:20
字体:大 中 小
来源:转载
供稿:网友

PHP文件读取函式,代码如下:

  1. function PHP_Read($file_name) { 
  2. $fd=fopen($file_name,r); 
  3. while($bufline=fgets($fd, 4096)){ 
  4. $buf.=$bufline; 
  5. } 
  6. fclose($fd); 
  7. return $buf; 
  8. } 

PHP文件写入函式,代码如下:

  1. function PHP_Write($file_name,$data,$method=”w”) { 
  2. $filenum=@fopen($file_name,$method); 
  3. flock($filenum,LOCK_EX); 
  4. $file_data=fwrite($filenum,$data); 
  5. fclose($filenum); 
  6. return $file_data; 
  7. } 

PHP静态页面生成函式,代码如下:

  1. function phptohtm($filefrom,$fileto,$u2u=1){ 
  2. if($u2u==1){ 
  3. $data=PHP_Read($filefrom); 
  4. }else{ 
  5. $data=$filefrom; 
  6. } 
  7. PHP_Write($fileto,$data); 
  8. return true; 
  9. } 

PHP指定条件信息数量检索函式,代码如下:

  1. function rec_exist($table,$where){ 
  2. $query=”select count(*) as num from $table “.$where; 
  3. $result=mysql_query($query) or die(nerror(1)); 
  4. $rowcount=mysql_fetch_array($result); 
  5. $num=$rowcount["num"]; 
  6. if ($num==0){ 
  7. return false; 
  8. } 
  9. return $num; 
  10. } 

PHP目录删除函式,代码如下:

  1. function del_DIR($directory){ 
  2. $mydir=dir($directory); 
  3. while($file=$mydir->read()){ 
  4. if((is_dir(“$directory/$file”)) AND ($file!=”.”) AND ($file!=”..”)){ 
  5. del_DIR(“$directory/$file”); 
  6. }else{ 
  7. if(($file!=”.”) AND ($file!=”..”)){ 
  8. unlink(“$directory/$file”); 
  9. //echo “unlink $directory/$file ok “; 
  10. } 
  11. } 
  12. } 
  13. $mydir->close(); 
  14. rmdir($directory); 
  15. //echo “rmdir $directory ok “; 
  16. } 

PHP目录名称合法性检测函式,代码如下:

  1. function isen($str){ 
  2. $ret=”"; 
  3. for($i=0;$i $p=ord(substr($str,$i,1)); 
  4. if(($p<48 & $p!=45 & $p!=46) || ($p>57 & $p<65) || ($p>90 & $p<97 & $p!=95) || $p>122){ 
  5. nerror(“不符合规范!”); 
  6. } 
  7. $ret.=chr($p); 
  8. } 
  9. return $ret; 
  10. } 

PHP分页函数,代码如下:

  1. //分页函数 
  2. function splitlist($HALT,$LRLIST,$ECHOCNT,$paper,$table,$where,$page_id,$userid){ 
  3. global $splitstr,$sumcnt; 
  4. if($paper==”" || $sumcnt==”"){ 
  5. $query = “select count(*) as num from $table $where”; 
  6. $result = mysql_query($query); 
  7. $row = mysql_fetch_array($result); 
  8. $sumcnt=$row["num"]; 
  9. if($sumcnt==0){ 
  10. nerror(“该版内还没有选择发布新闻 !”); 
  11. } 
  12. $paper=1; 
  13. } 
  14. $sumpaper=($sumcnt-$sumcnt%$ECHOCNT)/$ECHOCNT; 
  15. if(($sumcnt%$ECHOCNT)!=0) $sumpaper+=1; 
  16. if($sumpaper==1 && $HALT==0) return($where); 
  17. $enwhere=base64_encode(base64_encode($where)); 
  18. if(($LRLIST*2+1) < $sumpaper){ 
  19. if(($paper-$LRLIST) < 2){ 
  20. $tract=1; 
  21. $sub=$LRLIST*2+1; 
  22. }else if(($paper+$LRLIST) >= $sumpaper){ 
  23. $tract=$sumpaper-($LRLIST*2); 
  24. $sub=$sumpaper; 
  25. }else{ 
  26. $tract=$paper-$LRLIST; 
  27. $sub=$paper+$LRLIST; 
  28. } 
  29. }else{ 
  30. $tract=1; 
  31. $sub=$sumpaper; 
  32. } 
  33. $uppaper=$paper-1; 
  34. $downpaper=$paper+1; 
  35. $startcnt=($paper-1)*$ECHOCNT; 
  36. $where.=” limit ${ startcnt },${ ECHOCNT }”; 
  37. if($tract > 1) { $splitstr=”【 << “; } 
  38. else $splitstr=”【 << “; 
  39. for($i=$tract;$i<=$sub;$i++){ 
  40. if ($i!=$paper) $splitstr.=”".$i.” “; 
  41. else $splitstr.=”".$i.” “; 
  42. } 
  43. if ($sub!=$sumpaper) $splitstr.=”>> 】”; 
  44. else $splitstr.=”>> 】”; 
  45. return($where); 
  46. } 

PHP图片文件上传函式,代码如下:

  1. function upload_img($UploadFile,$UploadFile_name,$UploadFile_size,$UploadPath,$max_size=64){ 
  2. //$TimeLimit=60; //设置超时限制时间 缺省时间为 30秒 设置为0时为不限时 
  3. //set_time_limit($TimeLimit); 
  4. if(($UploadFile!= “none” )&&($UploadFile != “” )){ 
  5. $FileName=$UploadPath.$UploadFile_name; 
  6. if($UploadFile_size <1024){ 
  7. $FileSize=”(string)$UploadFile_size” . “字节”; 
  8. }elseif($UploadFile_size <(1024 * $max_size)){ 
  9. $FileSize=number_format((double)($UploadFile_size / 1024), 1) . ” KB”; 
  10. }else{ 
  11. nerror(“文件超过限制大小!”); 
  12. } 
  13. //{ 
  14. //$FileSize=”number_format((double)($UploadFile_size” / (1024 * 1024)), 1) . ” MB”; 
  15. // } 
  16. if(!file_exists($FileName)){ 
  17. if(copy($UploadFile,$FileName)){ 
  18. return “$UploadFile_name ($FileSize)”; 
  19. }else{ 
  20. nerror(“文件 $UploadFile_name 上载失败!”); 
  21. } 
  22. unlink($UploadFile); 
  23. }else{ 
  24. nerror(“文件 $UploadFile_name 已经存在!”); 
  25. } 
  26. //set_time_limit(30); //恢复缺省超时设置 
  27. } 
  28. }  

以下是一些小技巧.

PHP如何判断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是否合法的例子,代码如下:

  1. $ip = ’192.168.0.84′; 
  2. $mask = ’255.255.255.0′; 
  3. $network = ’192.168.0′; 
  4. $ip = ip2long($ip); 
  5. $mask = ip2long($mask); 
  6. $network = ip2long($network); 
  7. if( ($ip & $mask) == $network) echo “valid ip and maskn”; 

PHP文件下载头部输出如何设定,代码如下:

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

PHP用header输出ftp下载方式,并且支持断点续传,一个例子:

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

PHP正则匹配中文

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

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

  1. function urlParse($str = ”){ 
  2. if (” == $str) return $str; 
  3. $types = array(“http”, “ftp”, “https”); 
  4. $replace = << 
  5. ”.htmlentities(’1′).htmlentities(’2′).” 
  6. EOPHP; 
  7. $ret = $str; 
  8. while(list(,$type) = each($types)){ 
  9. $ret = preg_replace(“|($type://)([^s]*)|ie “, $replace, $ret); 
  10. } 
  11. return $ret; 
  12. } 

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