2020630

    技术2022-07-10  98

    php常用方法总结

    php获取毫秒函数判断是否是https请求代码如下获取请求连接url时间戳格式化对查询结果进行排序把返回的数据集转换成Tree存储(流量)单位换算php获取中文字符拼音首字母

    php获取毫秒函数

    代码如下

    // A code block /* *返回字符串的毫秒数时间戳 */ function _get_total_millisecond() { $time = explode (" ", microtime () ); $time = $time [1] . ($time [0] * 1000); $time2 = explode ( ".", $time ); $time = $time2 [0]; return $time; }

    判断是否是https请求代码如下

    function is_https() { if ( !empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') { return true; } elseif ( isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' ) { return true; } elseif ( !empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') { return true; } return false; }

    获取请求连接url

    function get_url() { $sys_protocal ='https://'; $php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME']; $path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : ''; $relate_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $php_self.(isset($_SERVER['QUERY_STRING']) ? '?'.$_SERVER['QUERY_STRING'] : $path_info); return $sys_protocal.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '').$relate_url; }

    时间戳格式化

    /** * 时间戳格式化 * @param int $time * @return string 完整的时间显示 * @author huajie <banhuajie@163.com> */ function time_format($time = NULL, $format = 'Y-m-d H:i') { $time = $time === NULL ? time() : intval($time); return date($format, $time); }

    对查询结果进行排序

    /** * 对查询结果集进行排序 * @access public * @param array $list 查询结果 * @param string $field 排序的字段名 * @param array $sortby 排序类型 * asc正向排序 desc逆向排序 nat自然排序 * @return array */ function list_sort_by($list, $field, $sortby = 'asc') { if (is_array($list)) { $refer = $resultSet = array(); foreach ($list as $i => $data) { $refer[$i] = &$data[$field]; } switch ($sortby) { case 'asc': // 正向排序 asort($refer); break; case 'desc': // 逆向排序 arsort($refer); break; case 'nat': // 自然排序 natcasesort($refer); break; } foreach ($refer as $key => $val) { $resultSet[] = &$list[$key]; } return $resultSet; } return false; }

    把返回的数据集转换成Tree

    /** * 把返回的数据集转换成Tree * @param array $list 要转换的数据集 * @param string $pid parent标记字段 * @param string $level level标记字段 * @return array */ function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0) { // 创建Tree $tree = array(); if (is_array($list) && !is_object($list)) { // 创建基于主键的数组引用 $refer = array(); foreach ($list as $key => $data) { $refer[$data[$pk]] = &$list[$key]; } foreach ($list as $key => $data) { // 判断是否存在parent $parentId = $data[$pid]; if ($root == $parentId) { $tree[] = &$list[$key]; } else { if (isset($refer[$parentId])) { $parent = &$refer[$parentId]; $parent['childs'][] = $data['id']; $parent[$child][] = &$list[$key]; } } } } return $tree; }

    存储(流量)单位换算

    /** * 格式化字节大小 * @param number $size 字节数 * @param string $delimiter 数字和单位分隔符 * @return string 格式化后的带单位的大小 */ function format_bytes($size, $delimiter = '') { $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB'); for ($i = 0; $size >= 1024 && $i < 5; $i++) { $size /= 1024; } return round($size, 2) . $delimiter . $units[$i]; }

    php获取中文字符拼音首字母

    //php获取中文字符拼音首字母 function getFirstCharter($s0) { $fchar = ord($s0{0}); if ($fchar >= ord("A") and $fchar <= ord("z")) { return strtoupper($s0{0}); } $s1 = \iconv("UTF-8", "gb2312", $s0); $s2 = \iconv("gb2312", "UTF-8", $s1); if ($s2 == $s0) {$s = $s1;} else { $s = $s0;} $asc = ord($s{0}) * 256 + ord($s{1}) - 65536; if ($asc >= -20319 and $asc <= -20284) { return "A"; } if ($asc >= -20283 and $asc <= -19776) { return "B"; } if ($asc >= -19775 and $asc <= -19219) { return "C"; } if ($asc >= -19218 and $asc <= -18711) { return "D"; } if ($asc >= -18710 and $asc <= -18527) { return "E"; } if ($asc >= -18526 and $asc <= -18240) { return "F"; } if ($asc >= -18239 and $asc <= -17923) { return "G"; } if ($asc >= -17922 and $asc <= -17418) { return "H"; } if ($asc >= -17417 and $asc <= -16475) { return "J"; } if ($asc >= -16474 and $asc <= -16213) { return "K"; } if ($asc >= -16212 and $asc <= -15641) { return "L"; } if ($asc >= -15640 and $asc <= -15166) { return "M"; } if ($asc >= -15165 and $asc <= -14923) { return "N"; } if ($asc >= -14922 and $asc <= -14915) { return "O"; } if ($asc >= -14914 and $asc <= -14631) { return "P"; } if ($asc >= -14630 and $asc <= -14150) { return "Q"; } if ($asc >= -14149 and $asc <= -14091) { return "R"; } if ($asc >= -14090 and $asc <= -13319) { return "S"; } if ($asc >= -13318 and $asc <= -12839) { return "T"; } if ($asc >= -12838 and $asc <= -12557) { return "W"; } if ($asc >= -12556 and $asc <= -11848) { return "X"; } if ($asc >= -11847 and $asc <= -11056) { return "Y"; } if ($asc >= -11055 and $asc <= -10247) { return "Z"; } return null; } function PyFirst($zh) { $ret = ""; $s1 = \iconv("UTF-8", "gb2312", $zh); $s2 = \iconv("gb2312", "UTF-8", $s1); if ($s2 == $zh) {$zh = $s1;} for ($i = 0; $i < strlen($zh); $i++) { $s1 = substr($zh, $i, 1); $p = ord($s1); if ($p > 160) { $s2 = substr($zh, $i++, 2); $ret .= getFirstCharter($s2); } else { $ret .= $s1; } } return $ret; }
    Processed: 0.011, SQL: 9