簡單方便使用:
/** * 將多維數組轉爲一維數組 * @param array $arr * @return array */ function ArrMd2Ud($arr) { #將數值第一元素做爲容器,做地址賦值。 $ar_room = &$arr[key($arr)]; #第一容器不是數組進去轉呀 if (!is_array($ar_room)) { #轉爲成數組 $ar_room = array($ar_room); } #指針下移 next($arr); #遍歷 while (list($k, $v) = each($arr)) { #是數組就遞歸深挖,不是就轉成數組 $v = is_array($v) ? call_user_func(__FUNCTION__, $v) : array($v); #遞歸合併 $ar_room = array_merge_recursive($ar_room, $v); #釋放當前下標的數組元素 unset($arr[$k]); } return $ar_room; }
/** * 日期轉換成幾分鐘前 */ public function formatTime($date) { $timer = strtotime($date); $diff = $_SERVER['REQUEST_TIME'] - $timer; $day = floor($diff / 86400); $free = $diff % 86400; if($day > 0) { if(15 < $day && $day <30){ return "半個月前"; }elseif(30 <= $day && $day <90){ return "1個月前"; }elseif(90 <= $day && $day <187){ return "3個月前"; }elseif(187 <= $day && $day <365){ return "半年前"; }elseif(365 <= $day){ return "1年前"; }else{ return $day."天前"; } }else{ if($free>0){ $hour = floor($free / 3600); $free = $free % 3600; if($hour>0){ return $hour."小時前"; }else{ if($free>0){ $min = floor($free / 60); $free = $free % 60; if($min>0){ return $min."分鐘前"; }else{ if($free>0){ return $free."秒前"; }else{ return '剛剛'; } } }else{ return '剛剛'; } } }else{ return '剛剛'; } } }
/** * 截取長度 */ public function getSubString($rawString,$length='100',$etc = '...',$isStripTag=true){ $rawString = str_replace('_baidu_page_break_tag_', '', $rawString); $result = ''; if($isStripTag) $string = html_entity_decode(trim(strip_tags($rawString)), ENT_QUOTES, 'UTF-8'); else $string = trim($rawString); $strlen = strlen($string); for ($i = 0; (($i < $strlen) && ($length > 0)); $i++){ if ($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0')) { if ($length < 1.0){ break; } $result .= substr($string, $i, $number); $length -= 1.0; $i += $number - 1; }else{ $result .= substr($string, $i, 1); $length -= 0.5; } } if($isStripTag) $result = htmlspecialchars($result, ENT_QUOTES, 'UTF-8'); if ($i < $strlen){ $result .= $etc; } return $result; }
/* * ************************* * 生成隨機字符串,能夠本身擴展 //若想惟一,只需在開頭加上用戶id * $type能夠爲:upper(只生成大寫字母),lower(只生成小寫字母),number(只生成數字) * $len爲長度,定義字符串長度 * mark 2017/8/15 * ************************** */ function _random($type, $len = 0) { $new = ''; $string = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; //數據池 if ($type == 'upper') { for ($i = 0; $i < $len; $i++) { $new .= $string[mt_rand(36, 61)]; } return $new; } if ($type == 'lower') { for ($i = 0; $i < $len; $i++) { $new .= $string[mt_rand(10, 35)]; } return $new; } if ($type == 'number') { for ($i = 0; $i < $len; $i++) { $new .= $string[mt_rand(0, 9)]; } return $new; } }
//計算該月有幾天 function getdaysInmonth($month, $year) { $days = ''; if ($month == 1 || $month == 3 || $month == 5 || $month == 7 || $month == 8 || $month == 10 || $month == 12) $days = 31; else if ($month == 4 || $month == 6 || $month == 9 || $month == 11) $days = 30; else if ($month == 2) { if (isLeapyear($year)) { $days = 29; } else { $days = 28; } } return ($days); } //判斷是否爲潤年 function isLeapyear($year) { if ((($year % 4) == 0) && (($year % 100) != 0) || (($year % 400) == 0)) { return (true); } else { return (false); } }
//生成訂單15位 function auto_order($ord = 0) { //自動生成訂單號 傳入參數爲0 或者1 0爲本地 1爲線上訂單 if ($ord == 0) { $str = '00' . time() . rand(1000, 9999); //00 本地訂單 } else { $str = '99' . time() . rand(1000, 9999); //11 線上訂單 } return $str; } //生成訂單15位 function auto_new_order($ord = 0) { srand(time()); //自動生成訂單號 傳入參數爲0 或者1 0爲本地 1爲線上訂單 if ($ord == 0) { $str = '00' . time() . mt_rand(100000,999999); //00 本地訂單 } else { $str = '11' . time() . mt_rand(100000,999999); //11 線上訂單 } return $str; }
/** * 檢測是否爲UTF8編碼 * @param $string 檢測字符串 * @return bool */ function is_utf8($string) { return preg_match('%^(?: [\x09\x0A\x0D\x20-\x7E] # ASCII | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 )*$%xs', $string); }
//處理json字符中的特殊字符 function getJsonToArr($result,$return_array=true) { $tempArr = NULL; $result = preg_replace('/([^\\\])(":)(\d{9,})(,")/i', '${1}${2}"${3}"${4}', $result); //taobao bug,number >2^32 $tempArr = json_decode($result, $return_array); if ($tempArr == NULL) { $patterns = array('/,+\s*\}/', '/,+\s*\]/', '/"\s+|\s+"/', '/\n|\r|\t/','/\\\/','/ss+/'); $replacements = array('}', ']', '"', ' ','',' '); $result = preg_replace($patterns, $replacements, $result); $tempArr = json_decode($result, $return_array); } return $tempArr; } //非法字符過濾函數 function has_unsafeword($str) { $regex = "/\/|\~|\!|\@|\#|\\$|\%|\^|\&|\*|\(|\)|\_|\+|\{|\}|\:|\<|\>|\?|\[|\]|\.|\/|\;|\'|\`|\=|\\\|\|/"; return preg_replace($regex,"",$str); } //去空格,以及字符添加斜槓 function _trim(&$value) { Return addslashes(trim($value)); }