国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

PHP愛好者來看:常用PHP代碼

2024-05-04 23:03:31
字體:
供稿:網(wǎng)友

這是一些使用頻率比較高的函數(shù)......

1.產(chǎn)生隨機(jī)字符串函數(shù)

function random($length) {
 $hash = @#@#;
 $chars = @#[email protected]#;
 $max = strlen($chars) - 1;
 mt_srand((double)microtime() * 1000000);
 for($i = 0; $i < $length; $i++) {
  $hash .= $chars[mt_rand(0, $max)];
 }
 return $hash;
}

2.截取一定長(zhǎng)度的字符串

注:該函數(shù)對(duì)gb2312使用有效

function wordscut($string, $length ,$sss=0) {
 if(strlen($string) > $length) {
               if($sss){
                      $length=$length - 3;
                      $[email protected]# [email protected]#;
                }
  for($i = 0; $i < $length; $i++) {
   if(ord($string[$i]) > 127) {
    $wordscut .= $string[$i].$string[$i + 1];
    $i++;
   } else {
    $wordscut .= $string[$i];
   }
  }
  return $wordscut.$addstr;
 }
 return $string;
}

3.取得客戶端ip地址

function getip(){
             if (getenv("http_client_ip") && strcasecmp(getenv("http_client_ip"), "unknown"))
                   $ip = getenv("http_client_ip");
             else if (getenv("http_x_forwarded_for") && strcasecmp(getenv("http_x_forwarded_for"), "unknown"))
                   $ip = getenv("http_x_forwarded_for");
             else if (getenv("remote_addr") && strcasecmp(getenv("remote_addr"), "unknown"))
                   $ip = getenv("remote_addr");
             else if (isset($_server[@#[email protected]#]) && $_server[@#[email protected]#] && strcasecmp($_server[@#[email protected]#], "unknown"))
                   $ip = $_server[@#[email protected]#];
             else
                  $ip = "unknown";
             return($ip);
}

4.創(chuàng)建相應(yīng)的文件夾

function createdir($[email protected]#@#)
{
        if (!is_dir($dir))
        {
            $temp = explode(@#/@#,$dir);
            $cur_dir = @#@#;
            for($i=0;$i<count($temp);$i++)
            {
                $cur_dir .= $temp[$i][email protected]#/@#;
                if (!is_dir($cur_dir))
                {
                @mkdir($cur_dir,0777);
                }
            }
        }
}

5.判斷郵箱地址

function checkemail($inaddress)
{
 return (ereg("^([a-za-z0-9_-])[email protected]([a-za-z0-9_-])+(/.[a-za-z0-9_-])+",$inaddress));
}

6.跳轉(zhuǎn)

function gotourl($[email protected]#@#,$[email protected]#@#,$[email protected]#@#)
{
    $html  ="<html><head>";
    if(!empty($url))
     $html .="<meta [email protected]#[email protected]# content=/"3;[email protected]#".$url."@#/">";
    $html .="<link [email protected]#../templates/[email protected]# type=text/css rel=stylesheet>";
    $html .="</head><body><br><br><br><br>";
    $html .="<table [email protected]#[email protected]# [email protected]#[email protected]# [email protected]#[email protected]# [email protected]#[email protected]# [email protected]#[email protected]#>";
 $html .="<tr><td [email protected]##[email protected]#>";
 $html .="<table [email protected]#[email protected]# [email protected]#[email protected]# [email protected]#[email protected]# [email protected]#100%@#>";
 $html .="<tr [email protected]#[email protected]#>";
 $html .="<td>".$title."</td></tr>";
 $html .="<tr [email protected]#[email protected]#><td [email protected]#[email protected]# [email protected]#[email protected]#>";
 $html .="<br>".$message."<br><br>";
    if (!empty($url))
     $html .="系統(tǒng)將在3秒后返回<br>如果您的瀏覽器不能自動(dòng)返回,請(qǐng)點(diǎn)擊[<a href=".$url." target=_self>這里</a>]進(jìn)入";
    else
     $html .="[<a [email protected]##@# [email protected]#history.go(-1)@#>返回</a>]";
    $html .="</td></tr></table></td></tr></table>";
 $html .="</body></html>";
 echo $html;
 exit;
}

7.分頁(兩個(gè)函數(shù)配合使用)

function getpage($sql,$page_size=20)
 {
      global $page,$totalpage,$sums;  //out param
      $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);
      if((!$page)||($page<1)) $page=1;
   $startpos = ($page-1)*$page_size;
   $sql .=" limit $startpos,$page_size ";
    return $sql;
 }
function showbar($string="")
{    
    global $page,$totalpage;
 $out="共<font ".$totalpage."[email protected]#[email protected]#><b>".$totalpage."</b></font>頁  ";
    $linknum =4;
    $start = ($page-round($linknum/2))>0 ? ($page-round($linknum/2)) : "1";
    $end   = ($page+round($linknum/2))<$totalpage ? ($page+round($linknum/2)) : $totalpage;
    $prestart=$start-1;
    $nextend=$end+1;
    if($page<>1)
 $out .= "<a [email protected]#?page=1&&".$string."@#title=第一頁>第一頁</a> ";
    if($start>1)
 $out.="<a [email protected]#?page=".$prestart."@# title=上一頁>..<<</a> ";
 for($t=$start;$t<=$end;$t++)
    {
     $out .= ($page==$t) ? "<font [".$t."][email protected]#[email protected]#><b>[".$t."]</b></font> " : "<a $t[email protected]#?page=$t&&".$string."@#>$t</a> ";
    }
 if($end<$totalpage)
 $out.="<a [email protected]#?page=".$nextend."&&".$string."@# title=下一頁>>>..</a>";
    if($page<>$totalpage)
   $out .= " <a [email protected]#?page=".$totalpage."&&".$string."@# title=最后頁>最后頁</a>";
   return $out;
}

8.獲取新插入數(shù)據(jù)的id

<?
mysql_insert_id();
?>

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 万全县| 丽江市| 县级市| 濮阳市| 普陀区| 固始县| 安龙县| 平陆县| 宿松县| 南陵县| 财经| 大同县| 股票| 古丈县| 龙南县| 马尔康县| 南岸区| 讷河市| 巴马| 沙洋县| 辽宁省| 永寿县| 宁海县| 田阳县| 澳门| 修水县| 衡山县| 宁晋县| 郴州市| 贺州市| 辽阳县| 凉山| 宣化县| 满洲里市| 金溪县| 深州市| 报价| 苏尼特右旗| 天门市| 时尚| 大同县|