av手机免费在线观看,国产女人在线视频,国产xxxx免费,捆绑调教一二三区,97影院最新理论片,色之久久综合,国产精品日韩欧美一区二区三区

php語(yǔ)言

PHP中常用的實(shí)例介紹

時(shí)間:2025-04-15 02:58:09 php語(yǔ)言 我要投稿
  • 相關(guān)推薦

PHP中常用的實(shí)例介紹

  PHP 獨(dú)特的語(yǔ)法混合了C、Java、Perl以及PHP自創(chuàng)的語(yǔ)法。它可以比CGI或者Perl更快速地執(zhí)行動(dòng)態(tài)網(wǎng)頁(yè)。下面小編給大家介紹PHP中常用的實(shí)例,歡迎閱讀!

  PHP中常用的實(shí)例介紹

  1.php解析url并得到url中的參數(shù)

  <?php

  $url = 'http://wwwXXX.com';

  $arr = parse_url($url);

  var_dump($arr);

  $arr_query = convertUrlQuery($arr['query']);

  var_dump($arr_query);

  var_dump(getUrlQuery($arr_query));

  /**

  * 將字符串參數(shù)變?yōu)閿?shù)組

  * @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;

  }

  /**

  * 將參數(shù)變?yōu)樽址?/p>

  * @param $array_query

  * @return string string 'm=content&c=index&a=lists&catid=6&area=0&author=0&h=0&region=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,利用正則表達(dá)式實(shí)現(xiàn)手機(jī)號(hào)碼中間4位用星號(hào)(*)替換顯示

  <?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);

  //實(shí)例:

  $phonenum = "13966778888";

  echo hidtel($phonenum);

  //最后輸出:139****8888

  3.php根據(jù)出生年月日計(jì)算生日 精確到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.'個(gè)月'.$d.'天';

  }elseif($Y != 0 && $m == 0 && $d != 0){

  return $Y.'年'.$d.'天';

  }else{

  return $Y.'年'.$m.'個(gè)月'.$d.'天';

  }

  }

  4.php判斷手機(jī)瀏覽還是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');//如果為手機(jī)端,執(zhí)行跳轉(zhuǎn)

  }

  else{

  header('location: ./web/index.php');//如果非手機(jī)端,執(zhí)行跳轉(zhuǎn)

  }

  5.時(shí)間戳轉(zhuǎn)時(shí)間格式,時(shí)間格式轉(zhuǎn)xx小時(shí)前

  <?php

  /**

  * 將時(shí)間轉(zhuǎn)換為 xx小時(shí)前

  * @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 + "小時(shí)前";

  } else if (d_hours <= 0 && d_minutes > 0) {

  return d_minutes + "分鐘前";

  } else {

  return pTime;

  }

  }


【PHP中常用的實(shí)例介紹】相關(guān)文章:

php中fsockopen用法實(shí)例06-20

php中return的用法實(shí)例分析10-27

php中實(shí)現(xiàn)回刪功能實(shí)例10-03

PHP中檢測(cè)ajax請(qǐng)求的代碼實(shí)例10-25

php畫圖實(shí)例07-16

php中try catch捕獲異常實(shí)例詳解07-29

php中curl模擬post請(qǐng)求小實(shí)例06-01

php常用的驗(yàn)證類以及正則實(shí)例08-27