PHP自定义函数帖——返回GD函数版本号(一)
- // 返回GD函数版本号
- function gd_version() {
- if (function_exists('gd_info')) {
- $GDArray = gd_info();
- $gd_version_number = $GDArray['GD Version'] ? $GDArray['GD Version'] : 0;
- unset($GDArray);
- } else {
- $gd_version_number = 0;
- }
- return $gd_version_number;
- }
--------------------------------------------------------
- //带汉字字符串截取
- function substrgb($in,$num){
- $pos=0;
- $out="";
- while($c=substr($in,$pos,1)){
- if($c=="/n") break;
- if(ord($c)>128){
- $out.=$c;
- $pos++;
- $c=substr($in,$pos,1);
- $out.=$c;
- }else{
- $out.=$c;
- }
- $pos++;
- if($pos>=$num) break;
- }
- if($out!=$in) $out = $out . "..";
- return $out;
- }
我喜欢这里---------因为我们是一家人:P :P :P
--------------------------------------------------------------
PHP自定义函数帖——返回内存占用(二)
- if (function_exists('memory_get_usage')) {
- $server['memory_info'] = get_real_size(memory_get_usage());
- }
自定义函数 get_real_size 结合上面函数返回当前主机内存占用情况
- function get_real_size($size) {
- $kb = 1024; // Kilobyte
- $mb = 1024 * $kb; // Megabyte
- $gb = 1024 * $mb; // Gigabyte
- $tb = 1024 * $gb; // Terabyte
- if($size < $kb) {
- return $size.' Byte';
- }else if($size < $mb) {
- return round($size/$kb,2).' KB';
- }else if($size < $gb) {
- return round($size/$mb,2).' MB';
- }else if($size < $tb) {
- return round($size/$gb,2).' GB';
- }else {
- return round($size/$tb,2).' TB';
- }
---------------------------------------------------
PHP自定义函数帖——返回GD函数版本号(三)
- //获得IP地址
- if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
- $onlineip = getenv('HTTP_CLIENT_IP');
- } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
- $onlineip = getenv('HTTP_X_FORWARDED_FOR');
- } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
- $onlineip = getenv('REMOTE_ADDR');
- } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
- $onlineip = $_SERVER['REMOTE_ADDR'];
- }
- $onlineip = addslashes($onlineip);
- @preg_match("/[/d/.]{7,15}/", $onlineip, $onlineipmatches);
- $onlineip = $onlineipmatches[0] ? $onlineipmatches[0] : 'unknown';
- unset($onlineipmatches);
---------------------------------------------------------
全角半角之间的切换(四)
- function f2a($Str) {
- $Queue = Array('0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9',
- 'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E', 'F' => 'F', 'G' => 'G', 'H' => 'H', 'I' => 'I', 'J' => 'J',
- 'K' => 'K', 'L' => 'L', 'M' => 'M', 'N' => 'N', 'O' => 'O', 'P' => 'P', 'Q' => 'Q', 'R' => 'R', 'S' => 'S', 'T' => 'T',
- 'U' => 'U', 'V' => 'V', 'W' => 'W', 'X' => 'X', 'Y' => 'Y', 'Z' => 'Z', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd',
- 'e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i', 'j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n',
- 'o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's', 't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x',
- 'y' => 'y', 'z' => 'z');
- return preg_replace("/([/xA3][/xB0-/xB9/xC1-/xDA/xE1-/xFA])/e", "/$Queue[//1]", $Str);
- }
用于全角半角之间的切换,经过测试发现个别中文会导致出错,所以最好用在英文或者数字的切换。
------------------------------------------------
PHP自定义函数帖——生成缓存文件(五)
对于程序的一些设置,如果生成缓存文件可以降低频繁读数据库的压力,
加快程序的执行。下面就是利用两个函数来生成缓存文件。
- function settings_recache()
- {
- global $DB,$db_prefix;
- $query=$DB->query("select * from {$db_prefix}settings");
- $content="/$options=array(";
- while($setting=$DB->fetch_array($query))
- {
- $content.="/t'".addslashes($setting['title'])."'=>'".addslashes($setting['value'])."',";
- }
- $content.=');';
- writetocache('settings',$content);
- }
- function writetocache($cachename,$cachedata)
- { if(in_array($cachename,array('archives','categories','hottags','links','newcomments','settings','statistics','stylevars')))
- {
- $cachedir="../cache/";
- $cachefile=$cachedir.'cache_'.$cachename.'.php';
- if(!is_dir($cachedir))
- {
- @mkdir($cachedir, 0777);
- }
- if($fp = @fopen($cachefile, 'wb'))
- {
- @fwrite($fp, "<?php//settings cache file//Created on ".date('Y-m-d H:i:s')."if(!defined('SOPHIA_ROOT')) exit('Access Denied');".$cachedata."?>");
- @fclose($fp);
- @chmod($cachefile, 0777);
- }
- else
- {
- echo 'Can not write to '.$cachename.' cache files, please check directory ./cache/ .';
- exit;
- }
- }
- }
[
-----------------------------------------------------------
PHP自定义函数帖——截取字符串(六)
利用这两个自定义函数截取字符串用在需要在首页等地方输出的而长度限制的字符串。
保证了各种状态下的截取,包括UTF-8,限制乱码的出现。
- //截取字数
- function trimmed_title($text, $limit=12) {
- if ($limit) {
- $val = csubstr($text, 0, $limit);
- return $val[1] ? $val[0]."..." : $val[0];
- } else {
- return $text;
- }
- function csubstr($text, $start=0, $limit=12) {
- if (function_exists('mb_substr')) {
- $more = (mb_strlen($text, 'UTF-8') > $limit) ? TRUE : FALSE;
- $text = mb_substr($text, 0, $limit, 'UTF-8');
- return array($text, $more);
- } elseif (function_exists('iconv_substr')) {
- $more = (iconv_strlen($text) > $limit) ? TRUE : FALSE;
- $text = iconv_substr($text, 0, $limit, 'UTF-8');
- return array($text, $more);
- } else {
- preg_match_all("/[/x01-/x7f]|[/xc2-/xdf][/x80-/xbf]|/xe0[/xa0-/xbf][/x80-/xbf]|[/xe1-/xef][/x80-/xbf][/x80-/xbf]|/xf0[/x90-/xbf][/x80-/xbf][/x80-/xbf]|[/xf1-/xf7][/x80-/xbf][/x80-/xbf][/x80-/xbf]/", $text, $ar);
- if(func_num_args() >= 3) {
- if (count($ar[0])>$limit) {
- $more = TRUE;
- $text = join("",array_slice($ar[0],0,$limit))."...";
- } else {
- $more = FALSE;
- $text = join("",array_slice($ar[0],0,$limit));
- }
- } else {
- $more = FALSE;
- $text = join("",array_slice($ar[0],0));
- }
- return array($text, $more);
- }
--------------------------------------------------------------------------
生成时间
function maketime($date)
{
if($date){
list($year,$month,$day) = explode("-",$date);
return mktime(0,0,0,$month,$day,$year);
}
return "";
}
生成目录:
function Directory($dir){ // force directory structure
return is_dir($dir) or (Directory(dirname($dir)) and mkdir($dir, 0777));
}
后退
function history_back($go="")
{
if($go){
echo "<script language=/"javascript/">history.go(".$go."); </script>":
}
else{
echo "<script language=/"javascript/">history.go(-1);</script>":
}
}
消息提示
function message($C_alert,$I_goback='') {
if(!empty($I_goback)) {
echo "<script>alert('$C_alert');window.location.href='$I_goback';</script>";
} else {
echo "<script>alert('$C_alert');</script>";
}
}
/**
* 截取中文部分字符串
*
* 截取指定字符串指定长度的函数,该函数可自动判定中英文,不会出现乱码
*
* @access public
* @param string $str 要处理的字符串
* @param int $strlen 要截取的长度默认为10
* @param string $other 是否要加上省略号,默认会加上
* @return string
*/
function showtitle($str,$strlen=10,$other=true) {
$j = 0;
for($i=0;$i<$strlen;$i++)
if(ord(substr($str,$i,1))>0xa0) $j++;
if($j%2!=0) $strlen++;
$rstr=substr($str,0,$strlen);
if (strlen($str)>$strlen && $other)
return $rstr;
}
function dhtmlspecialchars($string) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = dhtmlspecialchars($val);
}
} else {
$string = str_replace('&', '&', $string);
$string = str_replace('"', '"', $string);
$string = str_replace('<', '<', $string);
$string = str_replace('>', '>', $string);
$string = preg_replace('/&(#/d;)/', '&/1', $string);
}
return $string;
}
/
function daddslashes($string, $force = 0) {
if(!$GLOBALS['magic_quotes_gpc'] || $force) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = daddslashes($val, $force);
}
} else {
$string = addslashes($string);
}
}
return $string;
}
/.
function random($length) {
$hash = '';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars) - 1;
mt_srand((double)microtime() * 1000000);
for($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}
//
function wordscut($string, $length ,$sss=0) {
if(strlen($string) > $length) {
if($sss){
$length=$length - 3;
$addstr=' ...';
}
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;
}
/
百分比
function percent($a,$b,$float="")
{
if($b){
return (round($a/$b,$float+2)*100)."%";
}
return "";
}
/*
获取当前目录下的所有文件的个数
*/
function countDirFiles($dir="")
{
$files = 0;
if(!$dir){
$dir=".";
}
if(is_dir($dir)){
if($handle = opendir($dir)) {
while(false !== ($file = readdir($handle))) {
if(is_file($file)) {
$files++;
}
}
closedir($handle);
}
}
return $files;
}
echo countDirFiles();
/*
获取当前目录下的所有文件
*/
function listFiles($dir="")
{
$files = array();
if(!$dir){
$dir=".";
}
if(is_dir($dir)){
if($handle = opendir($dir)) {
while(false !== ($file = readdir($handle))) {
if(is_file($file)) {
$files[]=$file;
}
}
closedir($handle);
}
}
return $files;
}
print_r(listFiles(get_dir().'www/'));
/*
获取当前目录相对于根目录的相对路径
*/
function get_dir()
{
$biasNum = substr_count($_SERVER['REQUEST_URI'], '/'); //用'/'分割当前路径字符串,并计算分割后的字符串数量
$relativePath = '../'; //初始化变量$relativePath为'./'
for ($i = 0; $i < ($biasNum - 1); $i ++) { //循环添加'../'
$relativePath .= '../';
}
return $relativePath;
}
php去掉字符串中的空格
我们平时用的trim($str)只能去掉两头的空格
如果我们需要去掉中间的空格,可以这样:
str_replace(chr(32),"",$str);
如:
$str="a b c";
echo trim($str)."/n"; // output 'a b c'
echo str_replace(chr(32),"",$str); //output 'abc'
生成时间
function maketime($date)
{
if($date){
list($year,$month,$day) = explode("-",$date);
return mktime(0,0,0,$month,$day,$year);
}
return "";
}
生成目录:
function Directory($dir){ // force directory structure
return is_dir($dir) or (Directory(dirname($dir)) and mkdir($dir, 0777));
}
后退
function history_back($go="")
{
if($go){
echo "<script language=/"javascript/">history.go(".$go."); </script>":
}
else{
echo "<script language=/"javascript/">history.go(-1);</script>":
}
}
消息提示
function message($C_alert,$I_goback='') {
if(!empty($I_goback)) {
echo "<script>alert('$C_alert');window.location.href='$I_goback';</script>";
} else {
echo "<script>alert('$C_alert');</script>";
}
}
/**
* 截取中文部分字符串
*
* 截取指定字符串指定长度的函数,该函数可自动判定中英文,不会出现乱码
*
* @access public
* @param string $str 要处理的字符串
* @param int $strlen 要截取的长度默认为10
* @param string $other 是否要加上省略号,默认会加上
* @return string
*/
function showtitle($str,$strlen=10,$other=true) {
$j = 0;
for($i=0;$i<$strlen;$i++)
if(ord(substr($str,$i,1))>0xa0) $j++;
if($j%2!=0) $strlen++;
$rstr=substr($str,0,$strlen);
if (strlen($str)>$strlen && $other)
return $rstr;
}
function dhtmlspecialchars($string) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = dhtmlspecialchars($val);
}
} else {
$string = str_replace('&', '&', $string);
$string = str_replace('"', '"', $string);
$string = str_replace('<', '<', $string);
$string = str_replace('>', '>', $string);
$string = preg_replace('/&(#/d;)/', '&/1', $string);
}
return $string;
}
/
function daddslashes($string, $force = 0) {
if(!$GLOBALS['magic_quotes_gpc'] || $force) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = daddslashes($val, $force);
}
} else {
$string = addslashes($string);
}
}
return $string;
}
/.
function random($length) {
$hash = '';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars) - 1;
mt_srand((double)microtime() * 1000000);
for($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}
//
function wordscut($string, $length ,$sss=0) {
if(strlen($string) > $length) {
if($sss){
$length=$length - 3;
$addstr=' ...';
}
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;
}
/
百分比
function percent($a,$b,$float="")
{
if($b){
return (round($a/$b,$float+2)*100)."%";
}
return "";
}
/*
获取当前目录下的所有文件的个数
*/
function countDirFiles($dir="")
{
$files = 0;
if(!$dir){
$dir=".";
}
if(is_dir($dir)){
if($handle = opendir($dir)) {
while(false !== ($file = readdir($handle))) {
if(is_file($file)) {
$files++;
}
}
closedir($handle);
}
}
return $files;
}
echo countDirFiles();
/*
获取当前目录下的所有文件
*/
function listFiles($dir="")
{
$files = array();
if(!$dir){
$dir=".";
}
if(is_dir($dir)){
if($handle = opendir($dir)) {
while(false !== ($file = readdir($handle))) {
if(is_file($file)) {
$files[]=$file;
}
}
closedir($handle);
}
}
return $files;
}
print_r(listFiles(get_dir().'www/'));
/*
获取当前目录相对于根目录的相对路径
*/
function get_dir()
{
$biasNum = substr_count($_SERVER['REQUEST_URI'], '/'); //用'/'分割当前路径字符串,并计算分割后的字符串数量
$relativePath = '../'; //初始化变量$relativePath为'./'
for ($i = 0; $i < ($biasNum - 1); $i ++) { //循环添加'../'
$relativePath .= '../';
}
return $relativePath;
}
<?php
function check_email($email) {
return preg_match("^[_/.0-9a-z-]+@([0-9a-z][0-9a-z-]+/.)+[a-z]{2,4}$^",$email)?true:false;
}
function check_name($name,$min=1,$max=100) {
$pattern = "|^[a-zA-Z_]{1}[a-zA-Z0-9_]{".($min-1).",".($max-1)."}$|";
return preg_match($pattern,$name) ? true:false;
}
function check_char($str,$num1=1,$num2=12){
return (preg_match("/^[a-zA-Z]{".$num1.",".$num2."}$/",$str))?true:false;
}
//模板替换解析函数
function replace($contents){
return preg_replace_callback('|{/$([a-zA-Z]{1,}[0-9_]{0,})/$}|',create_function( '$pattern','
global $$pattern[1];
return $$pattern[1];
'),$contents);
}
//模板区域替换函数
function subreplace($flag,$sub,$contents) {
$sub = preg_replace('|{/$'.$flag.'/$}(.+){/$//'.$flag.'/$}|sU',$sub,$contents);
return $sub;
}
//模板取得区域函数
function getsub($flag,$contents){
if(!preg_match('|{/$'.$flag.'/$}(.+){/$//'.$flag.'/$}|sU',$contents,$array))
return false;
return $array[1];
}
//读文件
function read($file){
if(file_exists($file))
return file_get_contents($file);
else
return "模板文件不存在.";
}
//写文件
function write($file,$contents){
$fp = fopen($file,"w");
while(flock($fp,LOCK_EX)){
stream_set_write_buffer($fp, 0);
$b = fwrite($fp,$contents);
flock($fp,LOCK_UN);
fclose($fp);
return $b;
}
return false;
}
function arrtrim($arr) {
foreach ($arr as &$value) $value = trim($value);
}
//处理显示字符
function html($arr) {
foreach ($arr as &$value) $value = nl2br(str_replace(" "," ",htmlspecialchars($value))).chr(0);
}
//取两个字符串之间的字符串的函数
function getsubstr($str,$char1,$char2) {
if(empty($str)||empty($char1)||empty($char2)) return false;
if(strpos($str, $char1)===false||!strpos($str, $char2)) return false;
$pos = strpos($str, $char1) + strlen ($char1);
$len = strpos($str, $char2 ,$pos) - strpos($str, $char1) -strlen ($char1);
if($len > 0) return substr($str,$pos,$len);
else return false;
}
//分页函数
function cutpage($number,$maxpnum,$pagnum,$page,$url,$name,$pagnumname = "pagnum",$pagename = "page") {
$nextpage = $page < $maxpnum ?"<a href='".$url.$pagnumname."=".$pagnum."&".$pagename."=".($page+1)."'>下一页</a>":"下一页";
$prepage = $page > 1?"<a href='".$url.$pagnumname."=".$pagnum."&".$pagename."=".($page-1)."'>上一页</a>":"上一页";
$result = "<table align='center'><tr><td>共 <b>".$number."</b> 个".$name." <a href='".$url.$pagnumname."=".$pagnum."'>首页</a> ".$prepage." ".$nextpage." <a href='".$url.$pagnumname."=".$pagnum."&".$pagename."=".$maxpnum."'>尾页</a> 页次:<strong><font color=red>".$page."</font>/".$maxpnum."</strong>页 <input type='text' name='MaxPerPage' size='3' maxlength='4' value='".$pagnum."' onKeyPress=/"if (event.keyCode==13) window.location='".$url.$pagnumname."='+this.value;/">个".$name."/页 转到第<input type='text' name='page' size='3' maxlength='5' value='".$page."' onKeyPress=/"if (event.keyCode==13) window.location='".$url.$pagnumname."=".$pagnum."&".$pagename."='+this.value;/">页</td></tr></table>";
return $result;
}
//图像变黑白处理
function grayjpg($resimg,$grayimg) {
$image = imagecreatefromjpeg($resimg);
$img_width = ImageSX($image);
$img_height = ImageSY($image);
for ($y = 0; $y <$img_height; $y++) {
for ($x = 0; $x <$img_width; $x++) {
$gray = (ImageColorAt($image, $x, $y) >> 8) & 0xFF;
imagesetpixel ($image, $x, $y, ImageColorAllocate ($image, $gray,$gray,$gray));
}
}
imagejpeg($image,$grayimg,80);
imagedestroy($image);
}
//限制图片显示大小
function show_pic($filename,$width=400){
list($w,$h)=getimagesize($filename);
if($w>$width){
$h1=intval($h*($width/$w));
$w1=$width;
}else{
$h1=$h;
$w1=$w;
}
return "<a href=/"#/" οnclick=/"javascript:window.open('$filename','','height=$h,width=$w')/"><img src='$filename' width='$w1' height='$h1' border=0></a>";
}
function getrand() {
return strftime("%Y%m%d%H%M%S").rand(1000, 9999);
}
//+------------------------------------------------------
// 函数:html2area
// 说明:将html转换为 textarea内容
// 传入:$str:需要转换的变量名称
// 传出:无html标记的稳步
//-------------------------------------------------------
function html2area($str)//将html转换为textarea
{
$str=str_replace('&',"&",$str);
$str=str_replace('"',""",$str);
$str=str_replace('>',">",$str);
$str=str_replace('<',"<",$str);
$str=str_replace("'","'",$str);
return $str;
}
//+------------------------------------------------------
// 函数:area2html :
// 说明:将textarea中的内容中中已经被转化的html标记转换为html标记
// 传入:$str:变量字符串
// 传出:已经被转化过的html标记
//-------------------------------------------------------
function area2html($str)//将在textarea中输入的内容转化为html
{
$str=str_replace("'","'",$str);
$str=str_replace("<",'<',$str);
$str=str_replace(">",'>',$str);
$str=str_replace(""",'"',$str);
$str=str_replace("&",'&',$str);
// $str=htmlspecialchars($str,ENT_QUOTES);
return $str;
}
function getIP () //获取用户IP地址
{
global $_SERVER;
if (getenv('HTTP_CLIENT_IP')) {
$ip = getenv('HTTP_CLIENT_IP');
} else if (getenv('HTTP_X_FORWARDED_FOR')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
} else if (getenv('REMOTE_ADDR')) {
$ip = getenv('REMOTE_ADDR');
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
function getOS () //获取用户操作系统
{
global $_SERVER;
$agent = $_SERVER['HTTP_USER_AGENT'];
$os = false;
if (eregi('win', $agent) && strpos($agent, '95')){
$os = 'Windows 95';
}
else if (eregi('win 9x', $agent) && strpos($agent, '4.90')){
$os = 'Windows ME';
}
else if (eregi('win', $agent) && ereg('98', $agent)){
$os = 'Windows 98';
}
else if (eregi('win', $agent) && eregi('nt 5.1', $agent)){
$os = 'Windows XP';
}
else if (eregi('win', $agent) && eregi('nt 5', $agent)){
$os = 'Windows 2000';
}
else if (eregi('win', $agent) && eregi('nt', $agent)){
$os = 'Windows NT';
}
else if (eregi('win', $agent) && ereg('32', $agent)){
$os = 'Windows 32';
}
else if (eregi('linux', $agent)){
$os = 'Linux';
}
else if (eregi('unix', $agent)){
$os = 'Unix';
}
else if (eregi('sun', $agent) && eregi('os', $agent)){
$os = 'SunOS';
}
else if (eregi('ibm', $agent) && eregi('os', $agent)){
$os = 'IBM OS/2';
}
else if (eregi('Mac', $agent) && eregi('PC', $agent)){
$os = 'Macintosh';
}
else if (eregi('PowerPC', $agent)){
$os = 'PowerPC';
}
else if (eregi('AIX', $agent)){
$os = 'AIX';
}
else if (eregi('HPUX', $agent)){
$os = 'HPUX';
}
else if (eregi('NetBSD', $agent)){
$os = 'NetBSD';
}
else if (eregi('BSD', $agent)){
$os = 'BSD';
}
else if (ereg('OSF1', $agent)){
$os = 'OSF1';
}
else if (ereg('IRIX', $agent)){
$os = 'IRIX';
}
else if (eregi('FreeBSD', $agent)){
$os = 'FreeBSD';
}
else if (eregi('teleport', $agent)){
$os = 'teleport';
}
else if (eregi('flashget', $agent)){
$os = 'flashget';
}
else if (eregi('webzip', $agent)){
$os = 'webzip';
}
else if (eregi('offline', $agent)){
$os = 'offline';
}
else {
$os = 'Unknown';
}
return $os;
}
function current_time() //得到当前时间
{
return date("Y-m-d H:i:s",time());
}
function htmlOptions($values,$output,$selected=""){ //下拉列表$values数值$output显示
$result="";
if(is_array($output)&&is_array($values)&&count($output)==count($values)){
for($i=0;$i<count($output);$i++){
$ck=$values[$i]==$selected?" selected ":"";
$result.="<option value=$values[$i] $ck>$output[$i]</option>/r/n";
}
}
return $result;
}
function htmloptionskv($array,$selected=''){ //下拉列表
if(!is_array($array)){
return false;
}
$result='';
foreach($array as $key=>$value){
$ck=$key==$selected?" selected ":"";
$result.="<option value=$key $ck >$value</option>";
}
return $result;
}
function showError($msg){ //抛出信息(一般为错误信息)
echo "<script language='JavaScript'>";
echo "alert('$msg');";
echo "</script>";
}
function pageBack($history=-1){ //返回到指定步骤
echo "<script language='JavaScript'>";
echo "window.history.go($history);";
echo "</script>";
}
//截取中文字符函数
string iconv_substr ( string str, int offset [, int length [, string charset]] )//
function cnsub($string,$sublen) {
return $sublen>=strlen($string) ? $string : substr($string,0,$sublen).chr(0);
}
function substring($str, $length = '19', $fix = '...')//汉字截取$length为汉字个数
{
if(mb_strlen($str, 'gb2312') <= $length)
{
$fix = '';
}
return mb_substr($str, 0, $length, 'gb2312') . $fix;
}
function back($str="") { //输出信息后返回到上一个页面
if($str) echo "<script>alert(/"$str/");window.location =/"javascript:history.go(-1)/";</script>";
else echo "<script>window.location =/"javascript:history.go(-1)/";</script>";
exit;
}
function gourl($url,$str="") { //输出信息后返回到链接页面
if($str) echo "<script>alert(/"$str!/");window.location =/"$url/";</script>";
else echo "<script>window.location =/"$url/";</script>";
exit;
}
function locatepage($page){ //用js跳转到指定页面
echo "<script>/r/n
window.location.href('".$page."');/r/n
</script>/r/n
";
exit;
}?>