Rumah > pembangunan bahagian belakang > tutorial php > 实例代码展示php常用的函数,实现一些功能总结

实例代码展示php常用的函数,实现一些功能总结

伊谢尔伦
Lepaskan: 2023-03-11 15:56:02
asal
1324 orang telah melayarinya

本文汇总了常用的PHP函数,包括获取客户端IP,字符串截取,下载等,详情请查看如下代码:

<?php
/**
 * 获取客户端IP
 * @return [string] [description]
 */
function getClientIp() {
 $ip = NULL;
 if (isset($_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;])) {
  $arr = explode(&#39;,&#39;, $_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;]);
  $pos = array_search(&#39;unknown&#39;,$arr);
  if(false !== $pos) unset($arr[$pos]);
  $ip = trim($arr[0]);
 }elseif (isset($_SERVER[&#39;HTTP_CLIENT_IP&#39;])) {
  $ip = $_SERVER[&#39;HTTP_CLIENT_IP&#39;];
 }elseif (isset($_SERVER[&#39;REMOTE_ADDR&#39;])) {
  $ip = $_SERVER[&#39;REMOTE_ADDR&#39;];
 }
 // IP地址合法验证
 $ip = (false !== ip2long($ip)) ? $ip : &#39;0.0.0.0&#39;;
 return $ip;
}

/**
 * 获取在线IP
 * @return String
 */
function getOnlineIp($format=0) {
 global $S_GLOBAL;
 if(empty($S_GLOBAL[&#39;onlineip&#39;])) {
  if(getenv(&#39;HTTP_CLIENT_IP&#39;) && strcasecmp(getenv(&#39;HTTP_CLIENT_IP&#39;), &#39;unknown&#39;)) {
   $onlineip = getenv(&#39;HTTP_CLIENT_IP&#39;);
  } elseif(getenv(&#39;HTTP_X_FORWARDED_FOR&#39;) && strcasecmp(getenv(&#39;HTTP_X_FORWARDED_FOR&#39;), &#39;unknown&#39;)) {
   $onlineip = getenv(&#39;HTTP_X_FORWARDED_FOR&#39;);
  } elseif(getenv(&#39;REMOTE_ADDR&#39;) && strcasecmp(getenv(&#39;REMOTE_ADDR&#39;), &#39;unknown&#39;)) {
   $onlineip = getenv(&#39;REMOTE_ADDR&#39;);
  } elseif(isset($_SERVER[&#39;REMOTE_ADDR&#39;]) && $_SERVER[&#39;REMOTE_ADDR&#39;] && strcasecmp($_SERVER[&#39;REMOTE_ADDR&#39;], &#39;unknown&#39;)) {
   $onlineip = $_SERVER[&#39;REMOTE_ADDR&#39;];
  }
  preg_match("/[\d\.]{7,15}/", $onlineip, $onlineipmatches);
  $S_GLOBAL[&#39;onlineip&#39;] = $onlineipmatches[0] ? $onlineipmatches[0] : &#39;unknown&#39;;
 }

 if($format) {
  $ips = explode(&#39;.&#39;, $S_GLOBAL[&#39;onlineip&#39;]);
  for($i=0;$i<3;$i++) {
   $ips[$i] = intval($ips[$i]);
  }
  return sprintf(&#39;%03d%03d%03d&#39;, $ips[0], $ips[1], $ips[2]);
 } else {
  return $S_GLOBAL[&#39;onlineip&#39;];
 }
}



/**
 * 获取url
 * @return [type] [description]
 */
function getUrl(){
 $pageURL = &#39;http&#39;;
 if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {
 $pageURL .= "s";
 }
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
 $pageURL .= $_SERVER["HTTP_HOST"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
 } else {
 $pageURL .= $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}

/**
 * 获取当前站点的访问路径根目录
 * @return [type] [description]
 */
function getSiteUrl() {
 $uri = $_SERVER[&#39;REQUEST_URI&#39;]?$_SERVER[&#39;REQUEST_URI&#39;]:($_SERVER[&#39;PHP_SELF&#39;]?$_SERVER[&#39;PHP_SELF&#39;]:$_SERVER[&#39;SCRIPT_NAME&#39;]);
 return &#39;http://&#39;.$_SERVER[&#39;HTTP_HOST&#39;].substr($uri, 0, strrpos($uri, &#39;/&#39;)+1);
}



/**
 * 字符串截取,支持中文和其他编码
 * @param [string] $str  [字符串]
 * @param integer $start [起始位置]
 * @param integer $length [截取长度]
 * @param string $charset [字符串编码]
 * @param boolean $suffix [是否有省略号]
 * @return [type]   [description]
 */
function msubstr($str, $start=0, $length=15, $charset="utf-8", $suffix=true) {
 if(function_exists("mb_substr")) {
  return mb_substr($str, $start, $length, $charset);
 } elseif(function_exists(&#39;iconv_substr&#39;)) {
  return iconv_substr($str,$start,$length,$charset);
 }
 $re[&#39;utf-8&#39;] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
 $re[&#39;gb2312&#39;] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
 $re[&#39;gbk&#39;] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
 $re[&#39;big5&#39;] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
 preg_match_all($re[$charset], $str, $match);
 $slice = join("",array_slice($match[0], $start, $length));
 if($suffix) {
  return $slice."…";
 }
 return $slice;
}

/**
 * php 实现js escape 函数
 * @param [type] $string [description]
 * @param string $encoding [description]
 * @return [type]   [description]
 */
function escape($string, $encoding = &#39;UTF-8&#39;){
 $return = null;
 for ($x = 0; $x < mb_strlen($string, $encoding);$x ++)
 {
 $str = mb_substr($string, $x, 1, $encoding);
 if (strlen($str) > 1) { // 多字节字符
  $return .= "%u" . strtoupper(bin2hex(mb_convert_encoding($str, &#39;UCS-2&#39;, $encoding)));
 } else {
  $return .= "%" . strtoupper(bin2hex($str));
 }
 }
 return $return;
}
/**
 * php 实现 js unescape函数
 * @param [type] $str [description]
 * @return [type]  [description]
 */
function unescape($str) {
 $str = rawurldecode($str);
 preg_match_all("/(?:%u.{4})|.{4};|\d+;|.+/U",$str,$r);
 $ar = $r[0];
 foreach($ar as $k=>$v) {
  if(substr($v,0,2) == "%u"){
   $ar[$k] = iconv("UCS-2","utf-8//IGNORE",pack("H4",substr($v,-4)));
  } elseif(substr($v,0,3) == "") {
   $ar[$k] = iconv("UCS-2","utf-8",pack("H4",substr($v,3,-1)));
  } elseif(substr($v,0,2) == "") {
   echo substr($v,2,-1)."";
   $ar[$k] = iconv("UCS-2","utf-8",pack("n",substr($v,2,-1)));
  }
 }
 return join("",$ar);
}

/**
 * 数字转人名币
 * @param [type] $num [description]
 * @return [type]  [description]
 */
function num2rmb ($num) {
 $c1 = "零壹贰叁肆伍陆柒捌玖";
 $c2 = "分角元拾佰仟万拾佰仟亿";
 $num = round($num, 2);
 $num = $num * 100;
 if (strlen($num) > 10) {
  return "oh,sorry,the number is too long!";
 }
 $i = 0;
 $c = "";
 while (1) {
  if ($i == 0) {
   $n = substr($num, strlen($num)-1, 1);
  } else {
   $n = $num % 10;
  }
  $p1 = substr($c1, 3 * $n, 3);
  $p2 = substr($c2, 3 * $i, 3);
  if ($n != &#39;0&#39; || ($n == &#39;0&#39; && ($p2 == &#39;亿&#39; || $p2 == &#39;万&#39; || $p2 == &#39;元&#39;))) {
   $c = $p1 . $p2 . $c;
  } else {
   $c = $p1 . $c;
  }
  $i = $i + 1;
  $num = $num / 10;
  $num = (int)$num;
  if ($num == 0) {
   break;
  }
 }
 $j = 0;
 $slen = strlen($c);
 while ($j < $slen) {
  $m = substr($c, $j, 6);
  if ($m == &#39;零元&#39; || $m == &#39;零万&#39; || $m == &#39;零亿&#39; || $m == &#39;零零&#39;) {
   $left = substr($c, 0, $j);
   $right = substr($c, $j + 3);
   $c = $left . $right;
   $j = $j-3;
   $slen = $slen-3;
  }
  $j = $j + 3;
 }
 if (substr($c, strlen($c)-3, 3) == &#39;零&#39;) {
  $c = substr($c, 0, strlen($c)-3);
 } // if there is a &#39;0&#39; on the end , chop it out
 return $c . "整";
}

/**
 * 特殊的字符
 * @param [type] $str [description]
 * @return [type]  [description]
 */
function makeSemiangle($str) {
 $arr = array(
  &#39;0&#39; => &#39;0&#39;, &#39;1&#39; => &#39;1&#39;, &#39;2&#39; => &#39;2&#39;, &#39;3&#39; => &#39;3&#39;, &#39;4&#39; => &#39;4&#39;,
  &#39;5&#39; => &#39;5&#39;, &#39;6&#39; => &#39;6&#39;, &#39;7&#39; => &#39;7&#39;, &#39;8&#39; => &#39;8&#39;, &#39;9&#39; => &#39;9&#39;,
  &#39;A&#39; => &#39;A&#39;, &#39;B&#39; => &#39;B&#39;, &#39;C&#39; => &#39;C&#39;, &#39;D&#39; => &#39;D&#39;, &#39;E&#39; => &#39;E&#39;,
  &#39;F&#39; => &#39;F&#39;, &#39;G&#39; => &#39;G&#39;, &#39;H&#39; => &#39;H&#39;, &#39;I&#39; => &#39;I&#39;, &#39;J&#39; => &#39;J&#39;,
  &#39;K&#39; => &#39;K&#39;, &#39;L&#39; => &#39;L&#39;, &#39;M&#39; => &#39;M&#39;, &#39;N&#39; => &#39;N&#39;, &#39;O&#39; => &#39;O&#39;,
  &#39;P&#39; => &#39;P&#39;, &#39;Q&#39; => &#39;Q&#39;, &#39;R&#39; => &#39;R&#39;, &#39;S&#39; => &#39;S&#39;, &#39;T&#39; => &#39;T&#39;,
  &#39;U&#39; => &#39;U&#39;, &#39;V&#39; => &#39;V&#39;, &#39;W&#39; => &#39;W&#39;, &#39;X&#39; => &#39;X&#39;, &#39;Y&#39; => &#39;Y&#39;,
  &#39;Z&#39; => &#39;Z&#39;, &#39;a&#39; => &#39;a&#39;, &#39;b&#39; => &#39;b&#39;, &#39;c&#39; => &#39;c&#39;, &#39;d&#39; => &#39;d&#39;,
  &#39;e&#39; => &#39;e&#39;, &#39;f&#39; => &#39;f&#39;, &#39;g&#39; => &#39;g&#39;, &#39;h&#39; => &#39;h&#39;, &#39;i&#39; => &#39;i&#39;,
  &#39;j&#39; => &#39;j&#39;, &#39;k&#39; => &#39;k&#39;, &#39;l&#39; => &#39;l&#39;, &#39;m&#39; => &#39;m&#39;, &#39;n&#39; => &#39;n&#39;,
  &#39;o&#39; => &#39;o&#39;, &#39;p&#39; => &#39;p&#39;, &#39;q&#39; => &#39;q&#39;, &#39;r&#39; => &#39;r&#39;, &#39;s&#39; => &#39;s&#39;,
  &#39;t&#39; => &#39;t&#39;, &#39;u&#39; => &#39;u&#39;, &#39;v&#39; => &#39;v&#39;, &#39;w&#39; => &#39;w&#39;, &#39;x&#39; => &#39;x&#39;,
  &#39;y&#39; => &#39;y&#39;, &#39;z&#39; => &#39;z&#39;,
  &#39;(&#39; => &#39;(&#39;, &#39;)&#39; => &#39;)&#39;, &#39;〔&#39; => &#39;[&#39;, &#39;〕&#39; => &#39;]&#39;, &#39;【&#39; => &#39;[&#39;,
  &#39;】&#39; => &#39;]&#39;, &#39;〖&#39; => &#39;[&#39;, &#39;〗&#39; => &#39;]&#39;, &#39;{&#39; => &#39;{&#39;, &#39;}&#39; => &#39;}&#39;, &#39;《&#39; => &#39;<&#39;,
  &#39;》&#39; => &#39;>&#39;,
  &#39;%&#39; => &#39;%&#39;, &#39;+&#39; => &#39;+&#39;, &#39;—&#39; => &#39;-&#39;, &#39;-&#39; => &#39;-&#39;, &#39;~&#39; => &#39;-&#39;,
  &#39;:&#39; => &#39;:&#39;, &#39;。&#39; => &#39;.&#39;, &#39;、&#39; => &#39;,&#39;, &#39;,&#39; => &#39;.&#39;, &#39;、&#39; => &#39;.&#39;,
  &#39;;&#39; => &#39;;&#39;, &#39;?&#39; => &#39;?&#39;, &#39;!&#39; => &#39;!&#39;, &#39;…&#39; => &#39;-&#39;, &#39;‖&#39; => &#39;|&#39;,
  &#39;”&#39; => &#39;"&#39;, &#39;“&#39; => &#39;"&#39;, &#39;&#39;&#39; => &#39;`&#39;, &#39;‘&#39; => &#39;`&#39;, &#39;|&#39; => &#39;|&#39;, &#39;〃&#39; => &#39;"&#39;,
  &#39; &#39; => &#39; &#39;,&#39;.&#39; => &#39;.&#39;);
 return strtr($str, $arr);
}

/**
 * 下载
 * @param [type] $filename [description]
 * @param string $dir  [description]
 * @return [type]   [description]
 */
function downloads($filename,$dir=&#39;./&#39;){
 $filepath = $dir.$filename;
 if (!file_exists($filepath)){
  header("Content-type: text/html; charset=utf-8");
  echo "File not found!";
  exit;
 } else {
  $file = fopen($filepath,"r");
  Header("Content-type: application/octet-stream");
  Header("Accept-Ranges: bytes");
  Header("Accept-Length: ".filesize($filepath));
  Header("Content-Disposition: attachment; filename=".$filename);
  echo fread($file, filesize($filepath));
  fclose($file);
 }
}

/**
 * 创建一个目录树
 * @param [type] $dir [description]
 * @param integer $mode [description]
 * @return [type]  [description]
 */
function mkdirs($dir, $mode = 0777) {
 if (!is_dir($dir)) {
  mkdirs(dirname($dir), $mode);
  return mkdir($dir, $mode);
 }
 return true;
}
Salin selepas log masuk

Atas ialah kandungan terperinci 实例代码展示php常用的函数,实现一些功能总结. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan