0

0

php 的常用函数

php中文网

php中文网

发布时间:2016-07-25 08:50:32

|

956人浏览过

|

来源于php中文网

原创

php 的常用函数
  1. /**
  2. * 获取客户端IP
  3. * @return [string] [description]
  4. */
  5. function getClientIp() {
  6. $ip = NULL;
  7. if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  8. $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  9. $pos = array_search('unknown',$arr);
  10. if(false !== $pos) unset($arr[$pos]);
  11. $ip = trim($arr[0]);
  12. }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
  13. $ip = $_SERVER['HTTP_CLIENT_IP'];
  14. }elseif (isset($_SERVER['REMOTE_ADDR'])) {
  15. $ip = $_SERVER['REMOTE_ADDR'];
  16. }
  17. // IP地址合法验证
  18. $ip = (false !== ip2long($ip)) ? $ip : '0.0.0.0';
  19. return $ip;
  20. }
  21. /**
  22. * 获取在线IP
  23. * @return String
  24. */
  25. function getOnlineIp($format=0) {
  26. global $S_GLOBAL;
  27. if(empty($S_GLOBAL['onlineip'])) {
  28. if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
  29. $onlineip = getenv('HTTP_CLIENT_IP');
  30. } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
  31. $onlineip = getenv('HTTP_X_FORWARDED_FOR');
  32. } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
  33. $onlineip = getenv('REMOTE_ADDR');
  34. } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
  35. $onlineip = $_SERVER['REMOTE_ADDR'];
  36. }
  37. preg_match("/[\d\.]{7,15}/", $onlineip, $onlineipmatches);
  38. $S_GLOBAL['onlineip'] = $onlineipmatches[0] ? $onlineipmatches[0] : 'unknown';
  39. }
  40. if($format) {
  41. $ips = explode('.', $S_GLOBAL['onlineip']);
  42. for($i=0;$i $ips[$i] = intval($ips[$i]);
  43. }
  44. return sprintf('%03d%03d%03d', $ips[0], $ips[1], $ips[2]);
  45. } else {
  46. return $S_GLOBAL['onlineip'];
  47. }
  48. }
  49. /**
  50. * 获取url
  51. * @return [type] [description]
  52. */
  53. function getUrl(){
  54. $pageURL = 'http';
  55. if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {
  56. $pageURL .= "s";
  57. }
  58. $pageURL .= "://";
  59. if ($_SERVER["SERVER_PORT"] != "80") {
  60. $pageURL .= $_SERVER["HTTP_HOST"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
  61. } else {
  62. $pageURL .= $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
  63. }
  64. return $pageURL;
  65. }
  66. /**
  67. * 获取当前站点的访问路径根目录
  68. * @return [type] [description]
  69. */
  70. function getSiteUrl() {
  71. $uri = $_SERVER['REQUEST_URI']?$_SERVER['REQUEST_URI']:($_SERVER['PHP_SELF']?$_SERVER['PHP_SELF']:$_SERVER['SCRIPT_NAME']);
  72. return 'http://'.$_SERVER['HTTP_HOST'].substr($uri, 0, strrpos($uri, '/')+1);
  73. }
  74. /**
  75. * 字符串截取,支持中文和其他编码
  76. * @param [string] $str [字符串]
  77. * @param integer $start [起始位置]
  78. * @param integer $length [截取长度]
  79. * @param string $charset [字符串编码]
  80. * @param boolean $suffix [是否有省略号]
  81. * @return [type] [description]
  82. */
  83. function msubstr($str, $start=0, $length=15, $charset="utf-8", $suffix=true) {
  84. if(function_exists("mb_substr")) {
  85. return mb_substr($str, $start, $length, $charset);
  86. } elseif(function_exists('iconv_substr')) {
  87. return iconv_substr($str,$start,$length,$charset);
  88. }
  89. $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
  90. $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
  91. $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
  92. $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
  93. preg_match_all($re[$charset], $str, $match);
  94. $slice = join("",array_slice($match[0], $start, $length));
  95. if($suffix) {
  96. return $slice."…";
  97. }
  98. return $slice;
  99. }
  100. /**
  101. * php 实现js escape 函数
  102. * @param [type] $string [description]
  103. * @param string $encoding [description]
  104. * @return [type] [description]
  105. */
  106. function escape($string, $encoding = 'UTF-8'){
  107. $return = null;
  108. for ($x = 0; $x {
  109. $str = mb_substr($string, $x, 1, $encoding);
  110. if (strlen($str) > 1) { // 多字节字符
  111. $return .= "%u" . strtoupper(bin2hex(mb_convert_encoding($str, 'UCS-2', $encoding)));
  112. } else {
  113. $return .= "%" . strtoupper(bin2hex($str));
  114. }
  115. }
  116. return $return;
  117. }
  118. /**
  119. * php 实现 js unescape函数
  120. * @param [type] $str [description]
  121. * @return [type] [description]
  122. */
  123. function unescape($str) {
  124. $str = rawurldecode($str);
  125. preg_match_all("/(?:%u.{4})|.{4};|\d+;|.+/U",$str,$r);
  126. $ar = $r[0];
  127. foreach($ar as $k=>$v) {
  128. if(substr($v,0,2) == "%u"){
  129. $ar[$k] = iconv("UCS-2","utf-8//IGNORE",pack("H4",substr($v,-4)));
  130. } elseif(substr($v,0,3) == "") {
  131. $ar[$k] = iconv("UCS-2","utf-8",pack("H4",substr($v,3,-1)));
  132. } elseif(substr($v,0,2) == "") {
  133. echo substr($v,2,-1)."";
  134. $ar[$k] = iconv("UCS-2","utf-8",pack("n",substr($v,2,-1)));
  135. }
  136. }
  137. return join("",$ar);
  138. }
  139. /**
  140. * 数字转人名币
  141. * @param [type] $num [description]
  142. * @return [type] [description]
  143. */
  144. function num2rmb ($num) {
  145. $c1 = "零壹贰叁肆伍陆柒捌玖";
  146. $c2 = "分角元拾佰仟万拾佰仟亿";
  147. $num = round($num, 2);
  148. $num = $num * 100;
  149. if (strlen($num) > 10) {
  150. return "oh,sorry,the number is too long!";
  151. }
  152. $i = 0;
  153. $c = "";
  154. while (1) {
  155. if ($i == 0) {
  156. $n = substr($num, strlen($num)-1, 1);
  157. } else {
  158. $n = $num % 10;
  159. }
  160. $p1 = substr($c1, 3 * $n, 3);
  161. $p2 = substr($c2, 3 * $i, 3);
  162. if ($n != '0' || ($n == '0' && ($p2 == '亿' || $p2 == '万' || $p2 == '元'))) {
  163. $c = $p1 . $p2 . $c;
  164. } else {
  165. $c = $p1 . $c;
  166. }
  167. $i = $i + 1;
  168. $num = $num / 10;
  169. $num = (int)$num;
  170. if ($num == 0) {
  171. break;
  172. }
  173. }
  174. $j = 0;
  175. $slen = strlen($c);
  176. while ($j $m = substr($c, $j, 6);
  177. if ($m == '零元' || $m == '零万' || $m == '零亿' || $m == '零零') {
  178. $left = substr($c, 0, $j);
  179. $right = substr($c, $j + 3);
  180. $c = $left . $right;
  181. $j = $j-3;
  182. $slen = $slen-3;
  183. }
  184. $j = $j + 3;
  185. }
  186. if (substr($c, strlen($c)-3, 3) == '零') {
  187. $c = substr($c, 0, strlen($c)-3);
  188. } // if there is a '0' on the end , chop it out
  189. return $c . "整";
  190. }
  191. /**
  192. * 特殊的字符
  193. * @param [type] $str [description]
  194. * @return [type] [description]
  195. */
  196. function makeSemiangle($str) {
  197. $arr = array(
  198. '0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4',
  199. '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9',
  200. 'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E',
  201. 'F' => 'F', 'G' => 'G', 'H' => 'H', 'I' => 'I', 'J' => 'J',
  202. 'K' => 'K', 'L' => 'L', 'M' => 'M', 'N' => 'N', 'O' => 'O',
  203. 'P' => 'P', 'Q' => 'Q', 'R' => 'R', 'S' => 'S', 'T' => 'T',
  204. 'U' => 'U', 'V' => 'V', 'W' => 'W', 'X' => 'X', 'Y' => 'Y',
  205. 'Z' => 'Z', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd',
  206. 'e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i',
  207. 'j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n',
  208. 'o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's',
  209. 't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x',
  210. 'y' => 'y', 'z' => 'z',
  211. '(' => '(', ')' => ')', '〔' => '[', '〕' => ']', '【' => '[',
  212. '】' => ']', '〖' => '[', '〗' => ']', '{' => '{', '}' => '}', '《' => ' '》' => '>',
  213. '%' => '%', '+' => '+', '—' => '-', '-' => '-', '~' => '-',
  214. ':' => ':', '。' => '.', '、' => ',', ',' => '.', '、' => '.',
  215. ';' => ';', '?' => '?', '!' => '!', '…' => '-', '‖' => '|',
  216. '”' => '"', '“' => '"', '’' => '`', '‘' => '`', '|' => '|', '〃' => '"',
  217. ' ' => ' ','.' => '.');
  218. return strtr($str, $arr);
  219. }
  220. /**
  221. * 下载
  222. * @param [type] $filename [description]
  223. * @param string $dir [description]
  224. * @return [type] [description]
  225. */
  226. function downloads($filename,$dir='./'){
  227. $filepath = $dir.$filename;
  228. if (!file_exists($filepath)){
  229. header("Content-type: text/html; charset=utf-8");
  230. echo "File not found!";
  231. exit;
  232. } else {
  233. $file = fopen($filepath,"r");
  234. Header("Content-type: application/octet-stream");
  235. Header("Accept-Ranges: bytes");
  236. Header("Accept-Length: ".filesize($filepath));
  237. Header("Content-Disposition: attachment; filename=".$filename);
  238. echo fread($file, filesize($filepath));
  239. fclose($file);
  240. }
  241. }
  242. /**
  243. * 创建一个目录树
  244. * @param [type] $dir [description]
  245. * @param integer $mode [description]
  246. * @return [type] [description]
  247. */
  248. function mkdirs($dir, $mode = 0777) {
  249. if (!is_dir($dir)) {
  250. mkdirs(dirname($dir), $mode);
  251. return mkdir($dir, $mode);
  252. }
  253. return true;
  254. }
复制代码
  1. /**
  2. * 获取客户端IP
  3. * @return [string] [description]
  4. */
  5. function getClientIp() {
  6. $ip = NULL;
  7. if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  8. $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  9. $pos = array_search('unknown',$arr);
  10. if(false !== $pos) unset($arr[$pos]);
  11. $ip = trim($arr[0]);
  12. }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
  13. $ip = $_SERVER['HTTP_CLIENT_IP'];
  14. }elseif (isset($_SERVER['REMOTE_ADDR'])) {
  15. $ip = $_SERVER['REMOTE_ADDR'];
  16. }
  17. // IP地址合法验证
  18. $ip = (false !== ip2long($ip)) ? $ip : '0.0.0.0';
  19. return $ip;
  20. }
  21. /**
  22. * 获取在线IP
  23. * @return String
  24. */
  25. function getOnlineIp($format=0) {
  26. global $S_GLOBAL;
  27. if(empty($S_GLOBAL['onlineip'])) {
  28. if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
  29. $onlineip = getenv('HTTP_CLIENT_IP');
  30. } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
  31. $onlineip = getenv('HTTP_X_FORWARDED_FOR');
  32. } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
  33. $onlineip = getenv('REMOTE_ADDR');
  34. } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
  35. $onlineip = $_SERVER['REMOTE_ADDR'];
  36. }
  37. preg_match("/[\d\.]{7,15}/", $onlineip, $onlineipmatches);
  38. $S_GLOBAL['onlineip'] = $onlineipmatches[0] ? $onlineipmatches[0] : 'unknown';
  39. }
  40. if($format) {
  41. $ips = explode('.', $S_GLOBAL['onlineip']);
  42. for($i=0;$i $ips[$i] = intval($ips[$i]);
  43. }
  44. return sprintf('%03d%03d%03d', $ips[0], $ips[1], $ips[2]);
  45. } else {
  46. return $S_GLOBAL['onlineip'];
  47. }
  48. }
  49. /**
  50. * 获取url
  51. * @return [type] [description]
  52. */
  53. function getUrl(){
  54. $pageURL = 'http';
  55. if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {
  56. $pageURL .= "s";
  57. }
  58. $pageURL .= "://";
  59. if ($_SERVER["SERVER_PORT"] != "80") {
  60. $pageURL .= $_SERVER["HTTP_HOST"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
  61. } else {
  62. $pageURL .= $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
  63. }
  64. return $pageURL;
  65. }
  66. /**
  67. * 获取当前站点的访问路径根目录
  68. * @return [type] [description]
  69. */
  70. function getSiteUrl() {
  71. $uri = $_SERVER['REQUEST_URI']?$_SERVER['REQUEST_URI']:($_SERVER['PHP_SELF']?$_SERVER['PHP_SELF']:$_SERVER['SCRIPT_NAME']);
  72. return 'http://'.$_SERVER['HTTP_HOST'].substr($uri, 0, strrpos($uri, '/')+1);
  73. }
  74. /**
  75. * 字符串截取,支持中文和其他编码
  76. * @param [string] $str [字符串]
  77. * @param integer $start [起始位置]
  78. * @param integer $length [截取长度]
  79. * @param string $charset [字符串编码]
  80. * @param boolean $suffix [是否有省略号]
  81. * @return [type] [description]
  82. */
  83. function msubstr($str, $start=0, $length=15, $charset="utf-8", $suffix=true) {
  84. if(function_exists("mb_substr")) {
  85. return mb_substr($str, $start, $length, $charset);
  86. } elseif(function_exists('iconv_substr')) {
  87. return iconv_substr($str,$start,$length,$charset);
  88. }
  89. $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
  90. $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
  91. $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
  92. $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
  93. preg_match_all($re[$charset], $str, $match);
  94. $slice = join("",array_slice($match[0], $start, $length));
  95. if($suffix) {
  96. return $slice."…";
  97. }
  98. return $slice;
  99. }
  100. /**
  101. * php 实现js escape 函数
  102. * @param [type] $string [description]
  103. * @param string $encoding [description]
  104. * @return [type] [description]
  105. */
  106. function escape($string, $encoding = 'UTF-8'){
  107. $return = null;
  108. for ($x = 0; $x {
  109. $str = mb_substr($string, $x, 1, $encoding);
  110. if (strlen($str) > 1) { // 多字节字符
  111. $return .= "%u" . strtoupper(bin2hex(mb_convert_encoding($str, 'UCS-2', $encoding)));
  112. } else {
  113. $return .= "%" . strtoupper(bin2hex($str));
  114. }
  115. }
  116. return $return;
  117. }
  118. /**
  119. * php 实现 js unescape函数
  120. * @param [type] $str [description]
  121. * @return [type] [description]
  122. */
  123. function unescape($str) {
  124. $str = rawurldecode($str);
  125. preg_match_all("/(?:%u.{4})|.{4};|\d+;|.+/U",$str,$r);
  126. $ar = $r[0];
  127. foreach($ar as $k=>$v) {
  128. if(substr($v,0,2) == "%u"){
  129. $ar[$k] = iconv("UCS-2","utf-8//IGNORE",pack("H4",substr($v,-4)));
  130. } elseif(substr($v,0,3) == "") {
  131. $ar[$k] = iconv("UCS-2","utf-8",pack("H4",substr($v,3,-1)));
  132. } elseif(substr($v,0,2) == "") {
  133. echo substr($v,2,-1)."";
  134. $ar[$k] = iconv("UCS-2","utf-8",pack("n",substr($v,2,-1)));
  135. }
  136. }
  137. return join("",$ar);
  138. }
  139. /**
  140. * 数字转人名币
  141. * @param [type] $num [description]
  142. * @return [type] [description]
  143. */
  144. function num2rmb ($num) {
  145. $c1 = "零壹贰叁肆伍陆柒捌玖";
  146. $c2 = "分角元拾佰仟万拾佰仟亿";
  147. $num = round($num, 2);
  148. $num = $num * 100;
  149. if (strlen($num) > 10) {
  150. return "oh,sorry,the number is too long!";
  151. }
  152. $i = 0;
  153. $c = "";
  154. while (1) {
  155. if ($i == 0) {
  156. $n = substr($num, strlen($num)-1, 1);
  157. } else {
  158. $n = $num % 10;
  159. }
  160. $p1 = substr($c1, 3 * $n, 3);
  161. $p2 = substr($c2, 3 * $i, 3);
  162. if ($n != '0' || ($n == '0' && ($p2 == '亿' || $p2 == '万' || $p2 == '元'))) {
  163. $c = $p1 . $p2 . $c;
  164. } else {
  165. $c = $p1 . $c;
  166. }
  167. $i = $i + 1;
  168. $num = $num / 10;
  169. $num = (int)$num;
  170. if ($num == 0) {
  171. break;
  172. }
  173. }
  174. $j = 0;
  175. $slen = strlen($c);
  176. while ($j $m = substr($c, $j, 6);
  177. if ($m == '零元' || $m == '零万' || $m == '零亿' || $m == '零零') {
  178. $left = substr($c, 0, $j);
  179. $right = substr($c, $j + 3);
  180. $c = $left . $right;
  181. $j = $j-3;
  182. $slen = $slen-3;
  183. }
  184. $j = $j + 3;
  185. }
  186. if (substr($c, strlen($c)-3, 3) == '零') {
  187. $c = substr($c, 0, strlen($c)-3);
  188. } // if there is a '0' on the end , chop it out
  189. return $c . "整";
  190. }
  191. /**
  192. * 特殊的字符
  193. * @param [type] $str [description]
  194. * @return [type] [description]
  195. */
  196. function makeSemiangle($str) {
  197. $arr = array(
  198. '0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4',
  199. '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9',
  200. 'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E',
  201. 'F' => 'F', 'G' => 'G', 'H' => 'H', 'I' => 'I', 'J' => 'J',
  202. 'K' => 'K', 'L' => 'L', 'M' => 'M', 'N' => 'N', 'O' => 'O',
  203. 'P' => 'P', 'Q' => 'Q', 'R' => 'R', 'S' => 'S', 'T' => 'T',
  204. 'U' => 'U', 'V' => 'V', 'W' => 'W', 'X' => 'X', 'Y' => 'Y',
  205. 'Z' => 'Z', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd',
  206. 'e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i',
  207. 'j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n',
  208. 'o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's',
  209. 't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x',
  210. 'y' => 'y', 'z' => 'z',
  211. '(' => '(', ')' => ')', '〔' => '[', '〕' => ']', '【' => '[',
  212. '】' => ']', '〖' => '[', '〗' => ']', '{' => '{', '}' => '}', '《' => ' '》' => '>',
  213. '%' => '%', '+' => '+', '—' => '-', '-' => '-', '~' => '-',
  214. ':' => ':', '。' => '.', '、' => ',', ',' => '.', '、' => '.',
  215. ';' => ';', '?' => '?', '!' => '!', '…' => '-', '‖' => '|',
  216. '”' => '"', '“' => '"', '’' => '`', '‘' => '`', '|' => '|', '〃' => '"',
  217. ' ' => ' ','.' => '.');
  218. return strtr($str, $arr);
  219. }
  220. /**
  221. * 下载
  222. * @param [type] $filename [description]
  223. * @param string $dir [description]
  224. * @return [type] [description]
  225. */
  226. function downloads($filename,$dir='./'){
  227. $filepath = $dir.$filename;
  228. if (!file_exists($filepath)){
  229. header("Content-type: text/html; charset=utf-8");
  230. echo "File not found!";
  231. exit;
  232. } else {
  233. $file = fopen($filepath,"r");
  234. Header("Content-type: application/octet-stream");
  235. Header("Accept-Ranges: bytes");
  236. Header("Accept-Length: ".filesize($filepath));
  237. Header("Content-Disposition: attachment; filename=".$filename);
  238. echo fread($file, filesize($filepath));
  239. fclose($file);
  240. }
  241. }
  242. /**
  243. * 创建一个目录树
  244. * @param [type] $dir [description]
  245. * @param integer $mode [description]
  246. * @return [type] [description]
  247. */
  248. function mkdirs($dir, $mode = 0777) {
  249. if (!is_dir($dir)) {
  250. mkdirs(dirname($dir), $mode);
  251. return mkdir($dir, $mode);
  252. }
  253. return true;
  254. }
复制代码
  1. #完善cURL功能
  2. function xcurl($url,$ref=null,$post=array(),$ua="Mozilla/5.0 (X11; Linux x86_64; rv:2.2a1pre) Gecko/20110324 Firefox/4.2a1pre",$print=false) {
  3. $ch = curl_init();
  4. curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  5. if(!empty($ref)) {
  6. curl_setopt($ch, CURLOPT_REFERER, $ref);
  7. }
  8. curl_setopt($ch, CURLOPT_URL, $url);
  9. curl_setopt($ch, CURLOPT_HEADER, 0);
  10. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  11. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  12. if(!empty($ua)) {
  13. curl_setopt($ch, CURLOPT_USERAGENT, $ua);
  14. }
  15. if(count($post) > 0){
  16. curl_setopt($ch, CURLOPT_POST, 1);
  17. curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  18. }
  19. $output = curl_exec($ch);
  20. curl_close($ch);
  21. if($print) {
  22. print($output);
  23. } else {
  24. return $output;
  25. }
  26. }
复制代码
  1. /**
  2. * 根据一个时间戳得到详细信息
  3. * @param [type] $time [时间戳]
  4. * @return [type]
  5. * @author [yangsheng@yahoo.com]
  6. */
  7. function getDateInfo($time){
  8. $day_of_week_cn=array("日","一","二","三","四","五","六"); //中文星期
  9. $week_of_month_cn = array('','第1周','第2周','第3周','第4周','第5周','第6周');#本月第几周
  10. $tenDays= getTenDays(date('j',$time)); #获得旬
  11. $quarter = getQuarter(date('n',$time),date('Y',$time));# 获取季度
  12. $dimDate = array(
  13. 'date_key' => strtotime(date('Y-m-d',$time)), #日期时间戳
  14. 'date_day' => date('Y-m-d',$time), #日期YYYY-MM-DD
  15. 'current_year' => date('Y',$time),#数字年
  16. 'current_quarter' => $quarter['current_quarter'], #季度
  17. 'quarter_cn' =>$quarter['quarter_cn'],
  18. 'current_month' =>date('n',$time),#月
  19. 'month_cn' =>date('Y-m',$time), #月份
  20. 'tenday_of_month' =>$tenDays['tenday_of_month'],#数字旬
  21. 'tenday_cn' =>$tenDays['tenday_cn'],#中文旬
  22. 'week_of_month' =>ceil(date('j',$time)/7), #本月第几周
  23. 'week_of_month_cn' =>$week_of_month_cn[ceil(date('j',$time)/7)],#中文当月第几周
  24. 'day_of_year' =>date('z',$time)+1, #年份中的第几天
  25. 'day_of_month' =>date('j',$time),#得到几号
  26. 'day_of_week' =>date('w',$time)>0 ? date('w',$time):7,#星期几
  27. 'day_of_week_cn' =>'星期'.$day_of_week_cn[date('w',$time)],
  28. );
  29. return $dimDate;
  30. }
  31. /**
  32. * 获得日期是上中下旬
  33. * @param [int] $j [几号]
  34. * @return [array] [description]
  35. * @author [yangsheng@yahoo.com]
  36. */
  37. function getTenDays($j)
  38. {
  39. $j = intval($j);
  40. if($j 31){
  41. return false;#不是日期
  42. }
  43. $tenDays = ceil($j/10);
  44. switch ($tenDays) {
  45. case 1:#上旬
  46. return array('tenday_of_month'=>1,'tenday_cn'=>'上旬',);
  47. break;
  48. case 2:#中旬
  49. return array('tenday_of_month'=>2,'tenday_cn'=>'中旬',);
  50. break;
  51. default:#下旬
  52. return array('tenday_of_month'=>3,'tenday_cn'=>'下旬',);
  53. break;
  54. }
  55. return false;
  56. }
  57. /**
  58. * 根据月份获得当前第几季度
  59. * @param [int] $n [月份]
  60. * @param [int] $y [年]
  61. * @return [array] [description]
  62. */
  63. function getQuarter($n,$y=null){
  64. $n = intval($n);
  65. if($n 12){
  66. return false; #不是月份
  67. }
  68. $quarter = ceil($n/3);
  69. switch ($quarter) {
  70. case 1: #第一季度
  71. return array('current_quarter' => 1, 'quarter_cn'=>$y?$y.'-Q1':'Q1');
  72. break;
  73. case 2: #第二季度
  74. return array('current_quarter' => 2, 'quarter_cn'=>$y?$y.'-Q2':'Q2');
  75. break;
  76. case 3: #第三季度
  77. return array('current_quarter' => 3, 'quarter_cn'=>$y?$y.'-Q3':'Q3');
  78. break;
  79. case 4: #第四季度
  80. return array('current_quarter' => 4, 'quarter_cn'=>$y?$y.'-Q4':'Q4');
  81. break;
  82. }
  83. return false;
  84. }
复制代码


相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
云朵浏览器入口合集
云朵浏览器入口合集

本专题整合了云朵浏览器入口合集,阅读专题下面的文章了解更多详细地址。

0

2026.01.20

Java JVM 原理与性能调优实战
Java JVM 原理与性能调优实战

本专题系统讲解 Java 虚拟机(JVM)的核心工作原理与性能调优方法,包括 JVM 内存结构、对象创建与回收流程、垃圾回收器(Serial、CMS、G1、ZGC)对比分析、常见内存泄漏与性能瓶颈排查,以及 JVM 参数调优与监控工具(jstat、jmap、jvisualvm)的实战使用。通过真实案例,帮助学习者掌握 Java 应用在生产环境中的性能分析与优化能力。

20

2026.01.20

PS使用蒙版相关教程
PS使用蒙版相关教程

本专题整合了ps使用蒙版相关教程,阅读专题下面的文章了解更多详细内容。

62

2026.01.19

java用途介绍
java用途介绍

本专题整合了java用途功能相关介绍,阅读专题下面的文章了解更多详细内容。

87

2026.01.19

java输出数组相关教程
java输出数组相关教程

本专题整合了java输出数组相关教程,阅读专题下面的文章了解更多详细内容。

39

2026.01.19

java接口相关教程
java接口相关教程

本专题整合了java接口相关内容,阅读专题下面的文章了解更多详细内容。

10

2026.01.19

xml格式相关教程
xml格式相关教程

本专题整合了xml格式相关教程汇总,阅读专题下面的文章了解更多详细内容。

13

2026.01.19

PHP WebSocket 实时通信开发
PHP WebSocket 实时通信开发

本专题系统讲解 PHP 在实时通信与长连接场景中的应用实践,涵盖 WebSocket 协议原理、服务端连接管理、消息推送机制、心跳检测、断线重连以及与前端的实时交互实现。通过聊天系统、实时通知等案例,帮助开发者掌握 使用 PHP 构建实时通信与推送服务的完整开发流程,适用于即时消息与高互动性应用场景。

19

2026.01.19

微信聊天记录删除恢复导出教程汇总
微信聊天记录删除恢复导出教程汇总

本专题整合了微信聊天记录相关教程大全,阅读专题下面的文章了解更多详细内容。

160

2026.01.18

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
SciPy 教程
SciPy 教程

共10课时 | 1.2万人学习

R 教程
R 教程

共45课时 | 5.2万人学习

NumPy 教程
NumPy 教程

共44课时 | 2.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号