这篇文章给大家讲述了php实例相关的教程
1.php解析url并得到url中的参数
<?php $url = 'http://www.baidu.com/index.php?m=content&c=index&a=lists&catid=6&area=0&author=0&h=0®ion=0&s=1&page=1'; $arr = parse_url($url); var_dump($arr); $arr_query = convertUrlQuery($arr['query']); var_dump($arr_query); var_dump(getUrlQuery($arr_query)); /** * 将字符串参数变为数组 * @param $query * @return array array (size=10) 'm' => string 'content' (length=7) 'c' => string 'index' (length=5) 'a' => string 'lists' (length=5) 'catid' => string '6' (length=1) 'area' => string '0' (length=1) 'author' => string '0' (length=1) 'h' => string '0' (length=1) 'region' => string '0' (length=1) 's' => string '1' (length=1) 'page' => string '1' (length=1) */ function convertUrlQuery($query) { $queryParts = explode('&', $query); $params = array(); foreach ($queryParts as $param) { $item = explode('=', $param); $params[$item[0]] = $item[1]; } return $params; } /** * 将参数变为字符串 * @param $array_query * @return string string 'm=content&c=index&a=lists&catid=6&area=0&author=0&h=0®ion=0&s=1&page=1' (length=73) */ function getUrlQuery($array_query) { $tmp = array(); foreach($array_query as $k=>$param) { $tmp[] = $k.'='.$param; } $params = implode('&',$tmp); return $params; }
2,利用正则表达式实现手机号码中间4位用星号(*)替换显示
<?php //Method 1: function hidtel($phone){ $IsWhat = preg_match('/(0[0-9]{2,3}[\-]?[2-9][0-9]{6,7}[\-]?[0-9]?)/i',$phone); //固定电话 if($IsWhat == 1){ return preg_replace('/(0[0-9]{2,3}[\-]?[2-9])[0-9]{3,4}([0-9]{3}[\-]?[0-9]?)/i','$1****$2',$phone); }else{ return preg_replace('/(1[358]{1}[0-9])[0-9]{4}([0-9]{4})/i','$1****$2',$phone); } } //Method 2: $num = "13966778888" $str = substr_replace($num,'****',3,4); //实例: $phonenum = "13966778888"; echo hidtel($phonenum); //最后输出:139****88883.php根据出生年月日计算生日 精确到xxxx年xx月xx天
<?php function diffDate($date1,$date2){ $datestart= date('Y-m-d',strtotime($date1)); if(strtotime($datestart)>strtotime($date2)){ $tmp=$date2; $date2=$datestart; $datestart=$tmp; } list($Y1,$m1,$d1)=explode('-',$datestart); list($Y2,$m2,$d2)=explode('-',$date2); $Y=$Y2-$Y1; // 1 $m=$m2-$m1; // 0 $d=$d2-$d1; // -11 if($d<0){ $d+=(int)date('t',strtotime("-1 month $date2")); $m=$m--; } if($m<0){ $m+=12; $y=$y--; } if($Y == 0 && $m == 0 && $d != 0){ return $d.'天'; }elseif($Y == 0 && $m != 0 && $d != 0){ return $m.'个月'.$d.'天'; }elseif($Y != 0 && $m == 0 && $d != 0){ return $Y.'年'.$d.'天'; }else{ return $Y.'年'.$m.'个月'.$d.'天'; } }4.php判断手机浏览还是web浏览
<?php function isMobile(){ $useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''; $useragent_commentsblock=preg_match('|\(.*?\)|',$useragent,$matches)>0?$matches[0]:''; function CheckSubstrs($substrs,$text){ foreach($substrs as $substr) if(false!==strpos($text,$substr)){ return true; } return false; } $mobile_os_list=array('Google Wireless Transcoder','Windows CE','WindowsCE','Symbian','Android','armv6l','armv5','Mobile','CentOS','mowser','AvantGo','Opera Mobi','J2ME/MIDP','Smartphone','Go.Web','Palm','iPAQ'); $mobile_token_list=array('Profile/MIDP','Configuration/CLDC-','160×160','176×220','240×240','240×320','320×240','UP.Browser','UP.Link','SymbianOS','PalmOS','PocketPC','SonyEricsson','Nokia','BlackBerry','Vodafone','BenQ','Novarra-Vision','Iris','NetFront','HTC_','Xda_','SAMSUNG-SGH','Wapaka','DoCoMo','iPhone','iPod'); $found_mobile=CheckSubstrs($mobile_os_list,$useragent_commentsblock) || CheckSubstrs($mobile_token_list,$useragent); if ($found_mobile){ return true; }else{ return false; } } if (isMobile()){ header('location: ./app/index.php');//如果为手机端,执行跳转 } else{ header('location: ./web/index.php');//如果非手机端,执行跳转 }5.时间戳转时间格式,时间格式转xx小时前
<?php /** * 将时间转换为 xx小时前 * @param {Object} pTime */ function jsDateDiff(pTime) { var d_minutes, d_hours, d_days, d; var timeNow = parseInt(new Date().getTime() / 1000); pTime_new = new Date(pTime).getTime() / 1000; d = timeNow - pTime_new; d_days = parseInt(d / 86400); d_hours = parseInt(d / 3600); d_minutes = parseInt(d / 60); if (d_days > 0 && d_days < 4) { return d_days + "天前"; } else if (d_days <= 0 && d_hours > 0) { return d_hours + "小时前"; } else if (d_hours <= 0 && d_minutes > 0) { return d_minutes + "分钟前"; } else { return pTime; } }以上就是关于PHP实例相关内容,更多资讯欢迎关注编程学习网,每天都有新干货
扫码二维码 获取免费视频学习资料
- 本文固定链接: http://phpxs.com/post/7877/
- 转载请注明:转载必须在正文中标注并保留原文链接
- 扫码: 扫上方二维码获取免费视频资料
查 看2022高级编程视频教程免费获取