經常使用PHP函數

<?php
/*
 * 描述: 經常使用函數
 * 函數: getIP()獲取客服端IP
 *              escape(),unescape() 模擬javascript escape(),unescape()
 *              getExt() ,獲取文件後綴名
 *              length(),獲取中英文混合字符串長度
*               wordscut(),字符串剪切(中英)
 *做者:xieyu @ 2007-01-01
 *說明:代碼來源由本人收集,整理,以及部分原創
 *
 *
 */
function getIP(){
 if(getenv("HTTP_CLIENT_IP")) {
  $ip = getenv("HTTP_CLIENT_IP");
 } elseif(getenv("HTTP_X_FORWARDED_FOR")) {
  $ip = getenv("HTTP_X_FORWARDED_FOR");
 } elseif(getenv("REMOTE_ADDR")) {
  $ip = getenv("REMOTE_ADDR");
 } else {
  $ip = $_SERVER['REMOTE_ADDR'];
 }
 return $ip;
}
 
function escape($str){
 preg_match_all("/[x80-xff].|[x01-x7f]+/",$str,$r);
 $ar = $r[0];
 foreach($ar as $k=>$v) {
  if(ord($v[0]) < 128)
   $ar[$k] = rawurlencode($v);
  else
   $ar[$k] = "%u".bin2hex(iconv("GB2312","UCS-2",$v));
 }
 return implode("",$ar);
}
function unescape($str) {
 $str = urldecode($str);
 preg_match_all("/(?:%u.{4}|&#x.;|&#d+;|.+)/U",$str,$r);
 $ar = $r[0];
 foreach($ar as $k=>$v) {
  if(substr($v,0,2) == "%u")
   $ar[$k] = iconv("UCS-2","GB2312",pack("H4",substr($v,-4)));
  elseif(substr($v,0,3) == "&#x")
   $ar[$k] = iconv("UCS-2","GB2312",pack("H4",substr($v,3,-1)));
  elseif(substr($v,0,2) == "&#") {
   $ar[$k] = iconv("UCS-2","GB2312",pack("n",substr($v,2,-1)));
  }
 }
 return implode("",$ar);
}
// ####################### 防刷新處理 #######################
function pRefresh($second)
{
 $url_tail = ($_SERVER['QUERY_STRING']) ? "?".$_SERVER['QUERY_STRING']."" : "";
 $url = $_SERVER['PHP_SELF'].$url_tail;
 if(time() - $_COOKIE['VitisTime'] < $second)
 {
  echo "<meta http-equiv=\"refresh\" content=\"".$second.";URL=".$url."\">\n";
  echo "<span style=font-size:12px;>防刷新,".$second." 秒後自動跳轉...</span><p>\n";
  echo "<span style=font-size:12px;><a href=\"$url\"><font color=\"#000000\">若是瀏覽器沒有自動跳轉,請單擊這裏返回...</font></a></span>\n";
  exit;
 }
 $PostTime = time();
 setcookie("VitisTime", $PostTime);
}
//取文件後綴名 之一
function getExt_1($file_name){
 $ext = "";
 $pos = strrpos($file_name, ".");
 if ($pos !== false) $ext = substr($file_name, $pos+1);
 return ($ext);
}
//取文件後綴名 之二
//exp(www\abc\dd.htm): "dirname" = www\abc; "basename" = dd.htm; "extension" = htm
function getExt_2($file_name){
 $ext = pathinfo($file_name);
 $ext = strtolower($ext["extension"]); 
 return $ext;  
}
//取文件後綴名 之三
//無"."時將返回整個$file_name
function getExt_3($file_name){
 $ext = explode("." , $file_name);
 $num = count($ext) - 1;
 return $ext[$num];
}
 
//自動生產文件名
function getFileName($ext){
 $time_str = date("YmdHis");
 return $time_str.rand(100,999).$ext;
 
}
 
//計算字符串長度,能夠統計中文字符
function length($str){
 $len = strlen($str);
 $i = 0;
 while($i<$len){
  if(preg_match("/^[".chr(0xa1)."-".chr(0xff)."]+$/",$str[$i])){
   $i +=2;
  }else{
   $i += 1;
  }
  $n += 1;
 }
 return $n;
}
 
//字符剪切
function wordscut($string, $length) {
 if(strlen($string) > $length) {
  for($i = 0; $i < $length - 3; $i++) {
   if(ord($string[$i]) > 127) {
    $wordscut .= $string[$i].$string[$i + 1];
    $i++;
   }else{
    $wordscut .= $string[$i];
   }
  }
  return $wordscut.'...';
 }
 return $string;
}
 
//錯誤信息,並跳轉
function msg($str = "未知錯誤",$location=""){
 echo "<script language=javascript>";
 echo "alert('$str');";
 if($location == ""){
  echo "history.go(-1);";
 }elseif(is_int($location)){
  echo "history.go($location);";
 }else{
  echo "location='$location';";
 }
 echo "</script>";
 exit;
}
 
//自動跳轉 function goto($url,$time=0){  exit("<meta http-equiv=refresh content='$time;url=$url'>"); } ?>
相關文章
相關標籤/搜索