亚洲精品中文字幕无乱码_久久亚洲精品无码AV大片_最新国产免费Av网址_国产精品3级片

php語言

PHP中常用的實例介紹

時間:2024-09-04 06:26:38 php語言 我要投稿
  • 相關(guān)推薦

PHP中常用的實例介紹

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

  PHP中常用的實例介紹

  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á)式實現(xiàn)手機號碼中間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根據(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.'個月'.$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');//如果為手機端,執(zhí)行跳轉(zhuǎn)

  }

  else{

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

  }

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

  <?php

  /**

  * 將時間轉(zhuǎn)換為 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中常用的實例介紹】相關(guān)文章:

php中fsockopen用法實例06-20

php中return的用法實例分析10-27

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

PHP中檢測ajax請求的代碼實例10-25

php畫圖實例07-16

php常用的驗證類以及正則實例08-27

PHP實用的代碼實例08-12

php查詢mysql的實例09-09

PHP socket的配置及實例10-16

php簡單偽靜態(tài)實例09-16