在项目中经常用到的一些公共方法。
一个好的
程序员 要学会去收集一些公共类库,
function get_client_ip() {
if(getenv("HTTP_CLIENT_IP")) {
$onlineIP = getenv("HTTP_CLIENT_IP");
} elseif(getenv("HTTP_X_FORWARDED_FOR")) {
$onlineIP = getenv("HTTP_X_FORWARDED_FOR");
} elseif(getenv("REMOTE_ADDR")) {
$onlineIP = getenv("REMOTE_ADDR");
} else {
$onlineIP = $_SERVER['REMOTE_ADDR'];
}
return $onlineIP;
}
function formattime($num) {
$arr["hour"]="00";
if($num>=3600)
{ //小时
$arr["hour"] = sprintf("%d",($num/3600));
$num -= $arr['hour']*3600;
if($arr["hour"]<10){
$arr["hour"]="0". $arr["hour"];
}
}
$arr["minute"]="00";
if($num>=60)
{ //分
$arr["minute"] = sprintf("%d",($num/60));
$arr["second"] = sprintf("%d",$num-$arr['minute']*60);
if($arr["minute"]<10){
$arr["minute"]="0". $arr["minute"];
}
if($arr["second"]<10){
$arr["second"]="0". $arr["second"];
}
}
$arr['second']="00";
if($num<60&&$num>0){ $arr["second"] = $num;
if($arr["second"]<10){
$arr["second"]="0". $arr["second"];
}
}
return $arr["hour"].":".$arr["minute"].":".$arr["second"];
}
function cut_str($string, $sublen, $start = 0, $code = 'UTF-8'){
if($code == 'UTF-8'){
$pa = "/[\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]/";
preg_match_all($pa, $string, $t_string);
//print_r($t_string);
$i=$sublen;
foreach($t_string[0] as $k=>$chr){
//echo ord($chr)."<hr>";
//echo 'this'.$k.'this';
if(ord($chr)<=127&&$k<$i){
//echo 'hello<hr>';
if(ord($chr)>65&&ord($chr)<91){
}else{
$sublen++;
}
}
}
//echo $string.'<hr>'.$sublen.'<hr>';
if(count($t_string[0]) - $start > $sublen) {
return join('', array_slice($t_string[0], $start, $sublen))."...";
}else{
return join('', array_slice($t_string[0], $start, $sublen));
}
}else{
$start = $start*2;
$sublen = $sublen*2;
$strlen = $strlen($string);
$tmpstr = '';
for($i=0; $i< $strlen; $i++)
{
if($i>=$start && $i< ($start+$sublen))
{
if(ord(substr($string, $i, 1))>129)
{
$tmpstr.= substr($string, $i, 2);
}
else
{
$tmpstr.= substr($string, $i, 1);
}
}
if(ord(substr($string, i, 1))>129) $i++;
}
if(strlen($tmpstr)< $strlen ) $tmpstr.= "...";
return $tmpstr;
}
}
function setupSize($fileSize) {
//$fileSize=$fileSize*130.26;
$size=floatval($fileSize);
if($size == 0) {
return("0 Bytes");
}
$sizename = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
$i = floor(log($size, 1024));
//echo '<hr>'.$i;
return round($size/pow(1024, $i), 2). $sizename[$i];
}
/**
* 提示页面
* 对于程序内部出现的错误或者是用户操作的错误都可以以
* 此页面来提示。
* @param unknown_type $msg
* @param unknown_type $type
* time 跳转时间 秒
* url 跳转地址
* 错误的级别 是程序内部的错误还是用户操作的错误
* 来区分展示的不同的页面
*
*/
function showmsg($msg, $type, $time = 10, $url = "admin.php?a=dashboard") {
echo '<html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8">';
//echo '<meta http-equiv="refresh" content="'.$time.';url='.$url.'"> ';
echo '<title>信息提示页面</title></head>';
echo '<link href="public/css/style.css" rel="stylesheet" type="text/css" />';
echo '<body>';
if ($type == 0) {
echo '<div class="successaction">';
} else {
echo '<div class="failaction">';
}
echo '<h1>' . $msg . '</h1>';
echo '将于<span id="totalSecond" style="font-size:16pt; color:blue">' . $time . '</span>秒钟后自动跳转到新的页面!如果你没有<a href="' . $url . '">点击跳转</a>';
echo '<script language="javascript" type="text/javascript">';
echo 'var second = document.getElementById("totalSecond").textContent;
if (navigator.appName.indexOf("Explorer") > -1) {
second = document.getElementById("totalSecond").innerText;
} else {
second = document.getElementById("totalSecond").textContent;
}
setInterval("redirect()", 1000);
function redirect() {
if (second < 0) {
location.href = "' . $url . '";
} else {
if (navigator.appName.indexOf("Explorer") > -1) {
document.getElementById("totalSecond").innerText = second--;
} else {
document.getElementById("totalSecond").textContent = second--;
}
}
}
</script>';
echo '</body></html>';
die ();
}