A-A+

常用的PHP方法集合

2012年05月01日 PHP技术文章 暂无评论 阅读 61 views 次

十二个常用的PHP方法,有PHP产生随机字符串函数,截取一定长度的字符串,取得客户端IP地址,判断邮箱地址,分页(两个函数配合使用),获得当前的脚本网址,把全角数字转为半角数字,去除HTML标记,相对路径转化成绝对路径,取得所有链接,HTML表格的每行转为CSV格式数组,将HTML表格的每行每列转为数组,采集表格数据等方法,感觉还是比较实用的,用到时,直接拿来就行了,太适合像我这样的懒人了!

产生随机字符串函数

<?    
function random($length) {    
$hash = @#@#;    
$chars = @#ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
abcdefghijklmnopqrstuvwxyz@#;    
$max = strlen($chars) - 1;    
mt_srand((double)microtime() * 1000000);    
for($i = 0; $i < $length; $i++) { //OsPHP.COM.CN    
  $hash .= $chars&#91;mt_rand(0, $max)&#93;;    
}    
return $hash;    
} ?>

截取一定长度的字符串(该函数对GB2312使用有效)

<?    
function Wordscut($string, $length ,$sss=0) { strl
en($string) > $length) {    
               if($sss){    
                      $length=$length - 3;    
                      $addstr=@# ...@#;     
  if(}    
  for($i = 0; $i < $length; $i++) {    
   if(ord($string&#91;$i&#93;) > 127) {    
    $wordscut .= $string[$i].$string[$i + 1];     
    $i++;    
   } else {    
    $wordscut .= $string[$i];    
   }    
  }    
  return $wordscut.$addstr;    
}    
return $string;    
}?>

取得客户端IP地址

<?    
function GetIP(){    
             if (getenv("HTTP_CLIENT_IP") && strcasec
mp(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");
 getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR")
, "unknown"))    
                   $ip = getenv("REMOTE_ADDR");    
             else if (isset($_SERVER&#91;@#REMOTE_ADDR@#&#93;) &&
 $_SERVER&#91;@#REMOTE_ADDR@#&#93; && strcasecmp($_SERVER&#91;@#REMOT
E_ADDR@#&#93;, "unknown")) $ip = $_SERVER&#91;@#REMOTE_ADDR@#&#93;;    
             else    
                  $ip = "unknown";    
  
             return($ip);    
}?>

判断邮箱地址

<?    
function checkEmail($inAddress)    
{    
return (ereg("^(&#91;a-zA-Z0-9_-&#93;)+@(&#91;a-zA-Z0-9_-&#93;)+(.&#91;a-zA-Z
0-9_-&#93;)+",$inAddress));    
} ?>

分页(两个函数配合使用)

<?    
function getpage($sql,$page_size=20)
{    
      global $page,$totalpage,$sums; 
      $page = $_GET&#91;"page"&#93;;    
      //$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&#91;0&#93;;
 $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."color=@#red@#><b>".$totalpage."
</b></font>页  ";     
    $linkNum =4;    
    $start = ($page-round($linkNum/2))>0 ? ($page-round($lin
kNum/2)) : "1"; $end   = ($page+round($linkNum/2))<$totalpag
e ? ($page+round($linkNum/2)) : $totalpage;    
    $prestart=$start-1; $nextend=$end+1;    
    if($page<>1)     
$out .= "<a href=@#?page=1&&".$string."@#title=第一页>第一页</a> ";
    if($start>1) $out.="<a href=@#?page=".$prestart."@# 
title=上一页>..<<</a> ";    
  
for($t=$start;$t<=$end;$t++)    
    {    
     $out .= ($page==$t) ? "<font &#91;".$t."&#93;color=@#red@#><b>
[".$t."]</b></font> " : "<a $thref=@#?page=$t&&".$string."@#>
$t</a> "; $end<$totalpage)    
$out.="<a href=@#?page=".$nextend."&&".$string."@# title=下一页
>>>..</a>";    
    if($page<>$totalpage)    
   $out .= " <a href=@#?page=".$totalpage."&&".$string."@# tit
le=最后页>最后页</a>"; //开源代码OSPhP.COm.CN    
   return $out;    
}?>

获得当前的脚本网址

<?    
function get_php_url(){    
        if(!emptyempty($_server&#91;"REQUEST_URI"&#93;)){ //oSPHP.COM.CN    
                $scriptName = $_SERVER&#91;"REQUEST_URI"&#93;;    
                $nowurl = $scriptName;    
        }else{    
                $scriptName = $_SERVER&#91;"PHP_SELF"&#93;; $_SERVER&#91;"Q
UERY_STRING"&#93;)) $nowurl = $scriptName;    
                else $nowurl = $scriptName."?".$_SERVER&#91;"QUERY_
STRING"&#93;;    
        } $nowurl;    
}?>

把全角数字转为半角数字

<?    
function GetAlabNum($fnum){    
        $nums = array("0","1","2","3","4","5","6","7"
,"8","9"); $fnums = "0123456789";    
        for($i=0;$i<=9;$i++) $fnum = str_replace($nums&#91;$i&#93;
,$fnums&#91;$i&#93;,$fnum);     
        $fnum = ereg_replace("&#91;^0-9.&#93;|^0{1,}","",$fnum);    
        if($fnum=="") $fnum=0;    
        return $fnum;    
}?> 

去除HTML标记

<?    
function Text2Html($txt){    
        $txt = str_replace("  "," ",$txt);    
        $txt = str_replace("<","<",$txt);     
        $txt = str_replace(">",">",$txt);    
        $txt = preg_replace("/[rn]{1,}/isU","   
rn",$txt);    
        return $txt;    
}?>

相对路径转化成绝对路径

<?    
function relative_to_absolute($content, $feed_url) {     
    preg_match('/(http|https|ftp):///', $feed_url, $protocol);     
    $server_url = preg_replace("/(http|https|ftp|news):///", 
"", $feed_url);  //OSPHP.com.CN    
    $server_url = preg_replace("//.*/", "", $server_url);     
  
    if ($server_url == '') {     
        return $content;     
    }     
  
    if (isset($protocol&#91;0&#93;)) {      
        $new_content = preg_replace('/href="//', 'href="'.$prot
ocol&#91;0&#93;.$server_url.'/', $content);     
        $new_content = preg_replace('/src="//', 'src="'.$protoc
ol&#91;0&#93;.$server_url.'/', $new_content);  $new_content = $content;     
    }     
    return $new_content;     
} ?> 

取得所有链接

<?    
function get_all_url($code){     
        preg_match_all('/<as+href=&#91;"|'&#93;?(&#91;^>"' ]+)["|']?s*[^>]*>
([^>]+)</a>/i',$code,$arr);   
        return array('name'=>$arr[2],'url'=>$arr[1]);     
}?>

HTML表格的每行转为CSV格式数组

<?    
function get_tr_array($table) { //PHP开源代码    
        $table = preg_replace("'<td&#91;^>]*?>'si",'"',$table);    
        $table = str_replace("</td>",'",',$table);    
        $table = str_replace("</tr>","{tr}",$table); //去掉 HTML 标记
        $table = preg_replace("'<&#91;/!&#93;*?&#91;^<>]*?>'si","",$table);    
        //去掉空白字符      
        $table = preg_replace("'([rn])+'","",$table);     
  
            
        $table = str_replace(" ","",$table);    
        $table = str_replace(" ","",$table);    
  
        $table = explode(",{tr}",$table); array_pop($table);    
        return $table;    
}?>

将HTML表格的每行每列转为数组,采集表格数据

<?    
function get_td_array($table) {    
        $table = preg_replace("'<table&#91;^>]*?>'si","",$table); 
$table = preg_replace("'<tr&#91;^>]*?>'si","",$table);    
        $table = preg_replace("'<td&#91;^>]*?>'si","",$table);    
        $table = str_replace("</tr>","{tr}",$table); $table =
 str_replace("</td>","{td}",$table);    
        //去掉 HTML 标记     
        $table = preg_replace("'<&#91;/!&#93;*?&#91;^<>]*?>'si","",$table);
        //去掉空白字符      
        $table = preg_replace("'([rn])+'","",$table);
        $table = str_replace(" ","",$table);     
        $table = str_replace(" ","",$table);    
            
        $table = explode('{tr}', $table);    
        array_pop($table);     
        foreach ($table as $key=>$tr) {    
                $td = explode('{td}', $tr);    
                array_pop($td);    
            $td_array[] = $td; $td_array; }?>
标签:

给我留言