- 相关推荐
PHP中常用的实例介绍
PHP 独特的语法混合了C、Java、Perl以及PHP自创的语法。它可以比CGI或者Perl更快速地执行动态网页。下面小编给大家介绍PHP中常用的实例,欢迎阅读!
PHP中常用的实例介绍
1.php解析url并得到url中的参数
<?php
$url = '//wwwXXX.com';
$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****8888
3.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中常用的实例介绍】上海花千坊相关的文章:
php中fsockopen用法实例06-20
php中return的用法实例分析10-27
php中实现回删功能实例10-03
PHP中检测ajax请求的代码实例10-25
php画图实例07-16
php常用的验证类以及正则实例08-27
PHP实用的代码实例08-12
php查询mysql的实例09-09
PHP socket的配置及实例10-16
php简单伪静态实例09-16