<p> 这是一些使用频率比较高的函数......</p><p> 1.产生随机字符串函数</p><code>function random($length) {<br />$hash = @#@#;<br />$chars = @#ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz@#;<br />$max = strlen($chars) - 1;<br />mt_srand((double)microtime() * 1000000);<br />for($i = 0; $i < $length; $i++) {<br /> $hash .= $chars[mt_rand(0, $max)];<br />}<br />return $hash;<br />}</code><p> 2.截取一定长度的字符串</p><p> 注:该函数对GB2312使用有效</p><code>function wordscut($string, $length ,$sss=0) {<br />if(strlen($string) > $length) {<br /> if($sss){<br /> $length=$length - 3;<br /> $addstr=@# ...@#;<br /> }<br /> for($i = 0; $i < $length; $i++) {<br /> if(ord($string[$i]) > 127) {<br /> $wordscut .= $string[$i].$string[$i + 1];<br /> $i++;<br /> } else {<br /> $wordscut .= $string[$i];<br /> }<br /> }<br /> return $wordscut.$addstr;<br />}<br />return $string;<br />}</code><p> 3.取得客户端IP地址</p><code>function GetIP(){<br /> if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))<br /> $ip = getenv("HTTP_CLIENT_IP");<br /> else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))<br /> $ip = getenv("HTTP_X_FORWARDED_FOR");<br /> else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))<br /> $ip = getenv("REMOTE_ADDR");<br /> else if (isset($_SERVER[@#REMOTE_ADDR@#]) && $_SERVER[@#REMOTE_ADDR@#] && strcasecmp($_SERVER[@#REMOTE_ADDR@#], "unknown"))<br /> $ip = $_SERVER[@#REMOTE_ADDR@#];<br /> else<br /> $ip = "unknown";<br /> return($ip);<br />}</code><p> 4.创建相应的文件夹</p>
<p> </p>
<code>function createdir($dir=@#@#)<br />{<br /> if (!is_dir($dir))<br /> {<br /> $temp = explode(@#/@#,$dir);<br /> $cur_dir = @#@#;<br /> for($i=0;$i<count($temp);$i++)<br /> {<br /> $cur_dir .= $temp[$i].@#/@#;<br /> if (!is_dir($cur_dir))<br /> {<br /> @mkdir($cur_dir,0777);<br /> }<br /> }<br /> }<br />}</code><p> 5.判断邮箱地址</p><code>function checkEmail($inAddress)<br />{<br />return (ereg("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+",$inAddress));<br />}</code><p> 6.跳转</p><code>function gotourl($message=@#@#,$url=@#@#,$title=@#@#)<br />{<br /> $html ="<html><head>";<br /> if(!empty($url))<br /> $html .="<meta http-equiv=@#refresh@# content="3;url=@#".$url."@#">";<br /> $html .="<link href=@#../templates/style.css@# type=text/css rel=stylesheet>";<br /> $html .="</head><body><br><br><br><br>";<br /> $html .="<table cellspacing=@#0@# cellpadding=@#0@# border=@#1@# width=@#450@# align=@#center@#>";<br />$html .="<tr><td bgcolor=@##ffffff@#>";<br />$html .="<table border=@#1@# cellspacing=@#1@# cellpadding=@#4@# width=@#100%@#>";<br />$html .="<tr class=@#m_title@#>";<br />$html .="<td>".$title."</td></tr>";<br />$html .="<tr class=@#line_1@#><td align=@#center@# height=@#60@#>";<br />$html .="<br>".$message."<br><br>";<br /> if (!empty($url))<br /> $html .="系统将在3秒后返回<br>如果您的浏览器不能自动返回,请点击[<a href=".$url." target=_self>这里</a>]进入";<br /> else<br /> $html .="[<a href=@##@# onclick=@#history.go(-1)@#>返回</a>]";<br /> $html .="</td></tr></table></td></tr></table>";<br />$html .="</body></html>";<br />echo $html;<br />exit;<br />}</code><p> 7.分页(两个函数配合使用)</p>
<p> </p>
<code>function getpage($sql,$page_size=20)<br />{<br /> global $page,$totalpage,$sums; //out param<br /> $page = $_GET["page"];<br /> //$eachpage = $page_size;<br /> $pagesql = strstr($sql," from ");<br /> $pagesql = "select count(*) as ids ".$pagesql;<br /> $result = mysql_query($pagesql);<br /> if($rs = mysql_fetch_array($result)) $sums = $rs[0];<br /> $totalpage = ceil($sums/$page_size);<br /> if((!$page)||($page<1)) $page=1;<br /> $startpos = ($page-1)*$page_size;<br /> $sql .=" limit $startpos,$page_size ";<br /> return $sql;<br />}<br />function showbar($string="")<br />{ <br /> global $page,$totalpage;<br />$out="共<font ".$totalpage."color=@#red@#><b>".$totalpage."</b></font>页 ";<br /> $linkNum =4;<br /> $start = ($page-round($linkNum/2))>0 ? ($page-round($linkNum/2)) : "1";<br /> $end = ($page+round($linkNum/2))<$totalpage ? ($page+round($linkNum/2)) : $totalpage;<br /> $prestart=$start-1;<br /> $nextend=$end+1;<br /> if($page<>1)<br />$out .= "<a href=@#?page=1&&".$string."@#title=第一页>第一页</a> ";<br /> if($start>1)<br />$out.="<a href=@#?page=".$prestart."@# title=上一页>..<<</a> ";<br />for($t=$start;$t<=$end;$t++)<br /> {<br /> $out .= ($page==$t) ? "<font [".$t."]color=@#red@#><b>[".$t."]</b></font> " : "<a $thref=@#?page=$t&&".$string."@#>$t</a> ";<br /> }<br />if($end<$totalpage)<br />$out.="<a href=@#?page=".$nextend."&&".$string."@# title=下一页>>>..</a>";<br /> if($page<>$totalpage)<br /> $out .= " <a href=@#?page=".$totalpage."&&".$string."@# title=最后页>最后页</a>";<br /> return $out;<br />}</code><p> 8.获取新插入数据的ID</p></p><code><?<br />mysql_insert_id();<br />?></code></p></p>