PHP中常用函数的整理
2008-03-19 14:58:40 作者: 来源: 互联网 浏览次数: 39 文字大小:【 大】【 中】【 小】
简介:<?php//检查EMAIL的合法性function isEmail($email){ if (eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$",$email)) return true; else return false;}/*@字段处理 */functio ...
<?php
//检查EMAIL的合法性 function isEmail($email){ if (eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$",$email)) return true; else return false; }
/* @字段处理 */ function TextFilter($str){ $str = trim($str); $str = chop($str); $str = quotemeta($str); $str = nl2br( htmlspecialchars( $str) ); $str = addslashes($str); return $str; }
/* @ 取出数据时的处理情况 */ function opentext($str) { $str=StripSlashes($str); $str=StripSlashes($str); $str=nl2br($str); return $str; } /* @ showShort()处理字段的函数 */ Function csubstr($str,$start,$len) { $strlen=strlen($str); $clen=0; for($i=0;$i<$strlen;$i++,$clen++) { if ($clen>=$start+$len) break; if(ord(substr($str,$i,1))>0xa0) { if ($clen>=$start) $tmpstr.=substr($str,$i,2); $i++; } else { if ($clen>=$start) $tmpstr.=substr($str,$i,1); } } return $tmpstr; } Function showShort($str,$len) { $tempstr = csubstr($str,0,$len); //---------- 在此调用上面的函数 -------------- if ($str<>$tempstr) $tempstr .= "..."; //要以什么结尾,修改这里就可以. return $tempstr; }
/* @取得程序执行的时间微秒 */ function getmicrotime() { list($usec, $sec) = explode(" ",microtime()); return ((double)$usec + (double)$sec); } /* @get_ip() */ function get_ip(){ return getenv('REMOTE_ADDR'); }
/* @异常处理 */ function alert($msg,$flag=0){ echo "<meta http-equiv='Content-Type' content='text/html; charset=gb2312'>"; echo "<script>alert('".$msg."');history.back(-1)</script>"; if($flag) exit; }
/* @检查指定表名否在指定的数据库中 */ function table_exist($dbname='mysql',$table='user'){ global $conn; if($conn){ $flag = 0; $result = mysql_list_tables($dbname); for($i = 0;$i < mysql_num_rows($result);$i++){ if(mysql_tablename($result, $i)==$table){ $flag = 1; break; } } mysql_free_result($result); return $flag; } else { echo "<font size=2 color=red>还没有连接数据库或连接数据库出错</font>"; die(); } }
/* @取得URL及其参数的函数 */ function getUrl(){ $url="http://".$_SERVER["HTTP_HOST"]; if(isset($_SERVER["REQUEST_URI"])){ $url.=$_SERVER["REQUEST_URI"]; } else { $url.=$_SERVER["PHP_SELF"]; if(!empty($_SERVER["QUERY_STRING"])){ $url.="?".$_SERVER["QUERY_STRING"]; } } return $url; }
/* @ 用简单的数组处理很长的参数列表 */ function fruit_list(){ $fruits = func_get_args(); $fruit_list = ''; sort($fruits); $c = count($fruits); for($i=0;$i<$c;$i++){ $fruit_list.="&".$fruits[$i]; } return ($fruit_list); }
/** * 一段可以保持图片不变形的函数 * 函数名称: resizePic * 函数功能: 合理缩放图片 * 输入参数: $path ----------------- 图片路径 $maxwidth ------------- 最大宽度 $maxheight ------------ 最大高度 * 函数返回值: array * 其它说明: 2004-8-22 */ function resizePic($path,$maxwidth=120,$maxheight=120) { $info = array(); $pic_size = @GetImageSize($path['tmp_name']); $imagewidth = $pic_size['0']; $imageheight = $pic_size['1']; if ($imagewidth > $maxwidth) { $imageprop = ($maxwidth*100)/$imagewidth; $imagevsize = ($imageheight*$imageprop)/100 ; $imagewidth = $maxwidth; $imageheight = ceil($imagevsize); } if ($imageheight > $maxheight) { $imageprop = ($maxheight*100)/$imageheight; $imagehsize = ($imagewidth*$imageprop)/100 ; $imagewidth = ceil($imagehsize); $imageheight = $maxheight; } $info['width'] = $imagewidth; $info['height'] = $imageheight; Return $info; }
//创建目录 function createdir($dir='') { $this->dir=$dir?$dir:$this->dir;
if (!is_dir($this->dir)) { $temp = explode('/',$this->dir); $cur_dir = ''; for($i=0;$i<count($temp);$i++) { $cur_dir .= $temp[$i].'/'; if (!is_dir($cur_dir)) { @mkdir($cur_dir,0777); } } } } /** * function::deletedir() * 删除目录 * @param $file 目录名(不带/) * @return */ function deletedir($file) { if(file_exists($file)) { if(is_dir($file)) { $handle =opendir($file); while(false!==($filename=readdir($handle))) { if($filename!="."&&$filename!="..") $this->deletedir($file."/".$filename); } closedir($handle); rmdir($file); return true; }else{ unlink($file); } } }
/* @ 把数据合成字符串 一维数据 */ function arrtostr($arr,$tag=','){ $num_tmp = count($arr); if($num_tmp){ $indata =''; foreach($arr as $v){ $indata .=$tag.$v; } } $len = strlen($tag); $indata = substr($indata,$len); return $indata; }
/* *按一定的规律将数组中的某个值合并为一个字符串 * 二维数据 * 该函数可以将要组合的数据中重复的给过滤掉 */ function arrsTostr($arr,$str,$tag=',') { //$arr = array_unique($arr);//移除数据中重复的值 sort ($arr); reset ($arr); $num = count($arr); $newstr = ''; for($i=0;$i<$num;$i++){ if($arr[$i][$str]==$arr[$i-1][$str]){ $arr[$i][$str] = $arr[$i-1][$str]; $arr[$i-1][$str]=''; continue; } $newstr .=$tag.$arr[$i][$str]; } $len = strlen($tag); $newstr = substr($newstr,$len); return $newstr; } /* $arr = array(0=>array("id"=>"1"), 1=>array("id"=>"2"), 2=>array("id"=>"3")); echo arrsTostr($arr,"id"); */ function Alert_1($msg,$back=false,$flag=false,$close=false,$other=false){ $str = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">"; $str .= "<script>alert('".$msg."');"; if($back)$str .= $back?"history.back(-1);":""; if($other) $str .= $other; if($close) $str .= "window.close();"; $str .= "</script>"; echo $str; if($flag) die(); }
//取出二维数据中的一列值并转化为一维数组 function arrs2arr($arrs,$key){ $array = array(); foreach($arrs as $val){ foreach ($val as $k => $v) { if($k===$key)$array[]=$v; } } $array = resetkey(array_unique($array)); return $array; }
//重新整理键值 function resetkey($arr){ $array = array(); foreach($arr as $v){ $array[]=$v; } return $array; } ?>
|