一些经典的php函数方法代码整理

1,针对date_time类型为datatime的

header("Content-Type:text/html;charset=utf-8");  //设置系统的输出字符为utf-8
function timeFromNow($dateline) {  
   if(empty($dateline)) return false;  
   $seconds = time() - $dateline;  
   if ($seconds < 60){  
       return "1分钟前";  
   }elseif($seconds < 3600){  
       return floor($seconds/60)."分钟前";  
   }elseif($seconds  < 24*3600){  
       return floor($seconds/3600)."小时前";  
   }elseif($seconds < 48*3600){  
       return date("昨天 H:i", $dateline)."";  
   }else{  
       return date('Y-m-d', $dateline);  
   }  
}  

echo timeFromNow(strtotime("2012-8-31 9:15:13"));
echo "
";
echo timeFromNow(strtotime("2012-8-30 14:15:13"));

2,针对时间类型为int(11)

//时间转换函数
function tranTime($time) {  
   $rtime = date("m-d H:i",$time);  
   $htime = date("H:i",$time);  

   $time = time() - $time;  

   if ($time < 60) {  
       $str = '刚刚';  
   }  
   elseif ($time < 60 * 60) {  
       $min = floor($time/60);  
       $str = $min.'分钟前';  
   }  
   elseif ($time < 60 * 60 * 24) {  
       $h = floor($time/(60*60));  
       $str = $h.'小时前 '.$htime;  
   }  
   elseif ($time < 60 * 60 * 24 * 3) {  
       $d = floor($time/(60*60*24));  
       if($d==1)  
          $str = '昨天 '.$rtime;  
       else  
          $str = '前天 '.$rtime;  
   }  
   else {  
       $str = $rtime;  
   }  
   return $str;  
}  

3,对标签的过滤

function delTags($str)  
{  
   $farr = array(  
   "/<(/?)(script|i?frame|style|html|body|title|link|meta|form|input|embed|object|textarea|?|%)([^>]*?)>/isU",  
   "/(<[^>]*)on[a-zA-Z]+s*=([^>]*>)/isU"  
   );  
   $tarr = array(  
   "",  
   ""  
   );  
   $str = preg_replace( $farr,$tarr,$str);  
   return $str;  
}  

$str = "asdfasdfsd====
echo delTags($str);    //结果:asdfasdfsd====alert(1111)  
echo strip_tags($str); //结果:sdfasdfsd====alert(1111)

4,对中文的截取

function getStrTruncate($string, $length = 80, $etc = ''){  
   if ($length == 0) return '';  
   mb_internal_encoding("UTF-8");  

   $string = str_replace("n","",$string);  
   $strlen = mb_strwidth($string);  
   if ($strlen > $length) {  
       $etclen = mb_strwidth($etc);  
       $length = $length - $etclen;  
       $str=''; $n = 0;  
       for($i=0; $i<$length; $i++) {  
           $c = mb_substr($string, $i, 1);  
           $n += mb_strwidth($c);  
           if ($n>$length) { break; }  
           $str .= $c;  
       }  
       return $str.$etc;  
   } else {  
       return $string;  
   }  
}  

echo getStrTruncate("中文汉字截取",8);  //结果是中文汉字,utf8一个汉字对应二个字符 

5,获取IP问题

function getIp(){
if ($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"])
{
$ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
}
elseif ($HTTP_SERVER_VARS["HTTP_CLIENT_IP"])
{
$ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];
}
elseif ($HTTP_SERVER_VARS["REMOTE_ADDR"])
{
$ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];
}
elseif (getenv("HTTP_X_FORWARDED_FOR"))
{
$ip = getenv("HTTP_X_FORWARDED_FOR");
}
elseif (getenv("HTTP_CLIENT_IP"))
{
$ip = getenv("HTTP_CLIENT_IP");
}
elseif (getenv("REMOTE_ADDR"))
{
$ip = getenv("REMOTE_ADDR");
}
else
{
$ip = "Unknown";
}
return $ip ;
}

6,敏感字过滤

///敏感字过滤
function filterWords($string=''){
   $word_file=dirname(__FILE__)."/words.txt"; //字典文件在同级目录下words.txt
   if(file_exists($word_file)){
       $data=file_get_contents($word_file);
       $data=trim($data);//去掉末尾的回车空格
       $words_array=preg_split("/rn/", $data);//根据每一行分割成数组
       return str_replace($words_array,"???", $string);//过滤
   }
   return $string;
}

7,数字标准格式化,每三位数用逗号分割

function fNumber($number) {
if ( $number == '' ) Return "-";
$nlen = strlen($number);
while ( $nlen>3 ) {
$fNumber = ",". substr($number,$nlen-3,3).$fNumber;
$number = substr($number,0,-3);
$nlen = strlen($number);
}
if ( $nlen <= 3 ) {
$fNumber = $number.$fNumber;
}
Return $fNumber;
}
$number = 123456789;
echo fNumber($number);  /// 123,456,789

8,敏感字过滤 直接中英文

header("Content-Type:text/html;charset=utf-8");  //设置系统的输出字符为utf-8
function filterWords($string){
    $bad_words = explode('|', 'badword1|badword2|badword3|etc|seqing|电影|色');
    foreach ($bad_words as $naughty){
        $string = eregi_replace($naughty, "...#!@%*#...", $string);
    }
    return $string;
}
$name =  "aaaaaaaaaayahoo。。。。。seqing。。hdakbadword1fsd喝badword2咖啡集散地恢复看,色情单元,色彩,。色花";
echo filterWords($name);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值