查看: 6140|回复: 0
打印 上一主题 下一主题

PHP源代码集锦-Some Function

[复制链接]
跳转到指定楼层
1#
发表于 2007-10-5 16:09:33 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
台州网址导航
<?php
/*
函数名:GetPara

作  用:取得系统变量

简  介:习惯早期版本的朋友可能会习惯于直接通过 $para 来调用session, get, post, cookie 等类的变量,但是这在安全上会造成一定的隐患,也就是说可以通过 get 模式来欺骗系统,所以在 php 4.1.0 以后的版本中,registor_global 的默认值变成了 off ,也就是你不许通过系统数组来分类调用相关变量,但这也在一定程度上给习惯了原来模式的用户带来了不便,也使得一些早期的程序必须经过修改才可以在新的环境下运行,本函数基本上可以解决掉以上问题,同时通过分类激活避免用 get 伪装 post 等变量的问题。

效  果:是可以直接通过 $para 来调用相关变量(相当于 registor_global = on),好处是可以分类激活,避免通过 get 伪装 post 等信息!

方  法:模式一:GetPara("get","my_get_para") 取得名为 my_get_para 的 get 变量值
        模式一:GetPara("post") 声明所有 post 变量为可(像老版本php一样)直接调用的变量
        其  他:GetPara("file") ; GetPara("env") ; GetPara("server") 等...
*/
function GetPara($type = "get", $para = "") {
//Coded By Windy_sk 20030529 v1.5
$type = "_".strtoupper($type);
if(phpversion() < "4.1.0") {
  if($type = "_FILES") {
   $type = "HTTP_POST".$type;
  } elseif($type = "_REQUEST") {
   return $para?false:"";
  } else {
   $type = "HTTP".$type."_VARS";
  }
  @eval("global \${$type};");
}
eval("\$flag = isset(\${$type});");
if($flag) {
  eval("\$type = \${$type};");
} else {
  return $para?false:"";
}
if($para) {
  return isset($type[$para])?$type[$para]:"";
}
while(list($key, $value) = each($type)) {
  global $$key;
  $$key = $value;
}
return true;
}


/*
函数名:substrPro

作  用:取得字符串的指定部分,且不会出现将全角字符截断的现象

简  介:本函数是 substr 针对全角字符的扩展,避免截断全角字符,同时如果 $mode = true 的话,会将全角字符看作是一个字符!

方  法:substrPro("一1二三四4五5六七八8九十0", 2, 6)        -> "1二三四"
        substrPro("一1二三四4五5六七八8九十0", 2, 6, true)  -> "1二三四4五"
        注:暂不支持参数为负值
*/
function substrPro($Modi_Str, $start, $length, $mode = false){
//Coded By Windy_sk 20020603 v2.0
$n = 0;
for($i=0;$i<$start;$i++){
  if(ord(substr($Modi_Str,$i,1))>0xa0){
   if($mode){
    $start++;
    $i++;
   }
   $n++;
  }
}
if(!$mode)$start = $start + $n%2;
$The_length = $start+$length;
for($i=$start;$i<$The_length;$i++){
  if(ord(substr($Modi_Str,$i,1))>0xa0){
   $The_Str.=substr($Modi_Str,$i,2);
   $i++;
   if($mode) $The_length++;
  }else{
   $The_Str.=substr($Modi_Str,$i,1);
  }
}
return $The_Str;
}

/*
以下两个函数为取得程序的执行时间,并可定制精确度(最多精确到 1E-10 s)
*/
function getmicrotime() {
if(function_exists("microtime")) {
  list($usec, $sec) = explode(" ",microtime());
  return $usec + $sec;
} else {
  return time();
}
}

function gettimediff($time_start, $decimal = 3) {
$time_end = getmicrotime();
$time = (string)($time_end - $time_start);
$time = preg_replace("/^([\d]+.[\d]{".$decimal."})[\d]*$/","\\1",$time);
return $time;
}


/*
函数名:ob_handle

作  用:通过ob_start("ob_handle") 来处理缓存数据,并在 flush 前对其进行加工处理。

方  法:先设置 $ob_function 为函数名列表(以“;”作间隔,需均为字符串处理函数,且便两个数可为一,可以使自定义变量),再设置 ob_start("ob_handle") 调用本函数进行缓存控制,最后 ob_end_flush() 输出缓存。(注: $ob_function 在 ob_end_flush() 之前设置均有效!)
*/
$ob_function = "htmlspecialchars;trim";
function ob_handle ($content) {
//Coded By Windy_sk 20030510 v1.0
global $ob_function, $cache_file;
$f_list = split(";", $ob_function);
for($i=0; $i<count($f_list); $i++) {
  $temp = trim($f_list[$i]);
  if(function_exists($temp)) $content = $temp($content);
}
if(phpversion() > "4.0.6") $content = ob_gzhandler($content, 1);
return $content;
}


/*
函数名:RndKey

作  用:生成规定长度的随机字串

方  法:RndKey(8) -> "1d@5cDO("
*/
function RndKey($lng){
$char_list = array();
$char_list[] = "1234567890";
$char_list[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$char_list[] = "abcdefghijklmnopqrstuvwxyz";
$char_list[] = "!@^()_:+\-";
$char_length = count($char_list);
$Rnd_Key = "";
for($i=1; $i<=$lng; $i++){
  $Rnd_Str  = $char_list[rand(1,$char_length) - 1];
  $Rnd_Key .= substr($Rnd_Str, rand(0,strlen($Rnd_Str)-1), 1);
}
return($Rnd_Key);
}

/*
函数名:cut_words

作  用:将连续的文本按照全角字符和半角单词拆分
*/

function cut_words($str) {
//Coded By Windy_sk 20020805 v1.0
$str = str_replace("\r\n","\n",$str) . " ";
preg_match_all("/[\xa0-\xff]?./", $str, $arr1);
$arr1 = $arr1[0];
$arr2 = array();
$n = 0;
for($i=0; $i<count($arr1); $i++) {
  if(ord($arr1[$i])>=0xa0) {
   if(!empty($arr2[$n])) $n++;
   $arr2[$n++] = $arr1[$i];
  } elseif(preg_match("/\s/m", $arr1[$i])) {
   $arr2[++$n] = $arr1[$i];
  } else {
   $arr2[$n] .= $arr1[$i];
  }
}
return join("", $arr2);
}
?>
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

网站推广
关于我们
  • 台州朗动科技(Tzweb.com)拥有多年开发网站平台系统门户手机客户端等业务的成功经验。主要从事:政企网站,系统平台,微信公众号,各类小程序,手机APP客户端,浙里办微应用,浙政钉微应用、主机域名、虚拟空间、后期维护等服务,满足不同企业公司的需求,是台州地区领先的网络技术服务商!

Hi,扫描关注我

Copyright © 2005-2026 站长论坛 All rights reserved

Powered by 站长论坛 with TZWEB Update Techonolgy Support

快速回复 返回顶部 返回列表