<?phpphp
/*服務器
* Created on 2011-3-23app
* author 孤獨求學人ide
*/server
class common {ip
/*字符串
* 獲取ip地址get
*/it
function getIP() {io
if (isset ($_SERVER)) {
if (isset ($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$realip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
elseif (isset ($_SERVER['HTTP_CLIENT_IP'])) {
$realip = $_SERVER['HTTP_CLIENT_IP'];
} else {
$realip = $_SERVER['REMOTE_ADDR'];
}
} else {
if (getenv("HTTP_X_FORWARDED_FOR")) {
$realip = getenv("HTTP_X_FORWARDED_FOR");
}
elseif (getenv("HTTP_CLIENT_IP")) {
$realip = getenv("HTTP_CLIENT_IP");
} else {
$realip = getenv("REMOTE_ADDR");
}
}
return $realip;
}
/*
* 獲取服務器的時區
*/
function server_timezone(){
if(function_exists('date_default_timezone_get')){
return date_default_timezone_get();
}else{
return date('Z')/3600;
}
}
/*
* 獲取字符串的長度
*/
function str_len($str){
$length = strlen(preg_replace('/[\x00-\x7F]/','',$str));
if($length){
return strlen($str)-$length + intval($length/3)*2;
}else{
return strlen($str);
}
}
/*
* 截取字符串
* $str 將要截取的字符換
* $length 截取字符串的長度
* $append 是否添加省落號 默認是
*/
public function sub_str($str,$length=0,$append = true){
$str = trim($str);
$strlength = strlen($str);
if($length == 0 || $length >= $strlength){
return $str;
}elseif( $length < 0){
$length = $length + $strlength;
if($length < 0){
$length = $strlength;
}
}
if(function_exists("mb_substr")){
$newstr = mb_substr($str,0,$length,"gb2312");
}elseif (function_exists('iconv_substr')){
$newstr = iconv_substr($str, 0, $length, "gb2312");
}else
{
$newstr = substr($str, 0, $length);
}
if ($append && $str != $newstr){
$newstr .= '...';
}
return $newstr;
}
/*得到當前格林威治時間的時間戳
* @return integer
*/
function getTime(){
return (time() - date("Z"));
}
/*
* 建立像這樣的查詢條件 in('','');
*/
function db_create_in($itme_list,$feild_name){
/*if(empty($itme_list)){
return $feild_name."IN('')";
}else{
if(!is_array($feild_name)){
$itme_list = explode(",",$itme_list);
}
$itme_list = array_unique($itme_list);
$itme_list_tmp ="";
foreach($itme_list as $item){
if(!$item($item)){
$itme_list_tmp .= $itme_list_tmp?",'$item'":"'$item'";
}
}
if(empty($itme_list_tmp)){
return $feild_name."IN('')";
}else{
return $feild_name."IN('.$itme_list_tmp.')";
}
}*/
}
/*
* 檢查時間的格式是否正確
*/
function is_time($time){
$pattern = '/[\d]{4}-[\d]{1,2}-[\d]{1,2}\s[\d]{1,2}:[\d]{1,2}:[\d]{1,2}/';
return preg_match($pattern,$time);
}
}
?>