PHP常用類庫
數組類
class libArray
{
/**
* 多維數組合並
* @return array
*/
public static function merge ()
{
$args = func_get_args();
$array = [];
foreach ( $args as $arg ) {
if ( is_array($arg) ) {
foreach ( $arg as $k => $v ) {
if ( is_array($v) ) {
$array[$k] = isset($array[$k]) ? $array[$k] : [];
$array[$k] = self::merge($array[$k], $v);
} else {
$array[$k] = $v;
}
}
}
}
return $array;
}
/**
* 多維to一維
* [1=>['a'=>'v1']] to ['1|a'=>'v1']
* @param array $array
* @param string $delimiter
* @param string $key
* @return array
*/
public static function mTo1 (array $array, $delimiter = '|', $key = '')
{
$data = [];
if ( !is_array($array) ) {
return $data;
}
foreach ( $array as $k => $v ) {
$keyNew = trim($key.$delimiter.$k, $delimiter);
if ( is_array($v) ) {
$data = array_merge($data, self::mTo1($v, $delimiter, $keyNew));
} else {
$data[$keyNew] = $v;
}
}
return $data;
}
/**
* 數組排序
* @param array $array
* @param $column
* @param bool $reverse
* @return bool
*/
public static function sort (array &$array, $column, $reverse = FALSE)
{
$arrColumn = [];
foreach ( $array as $key => $val ) {
$arrColumn[$key] = $val[$column];
}
return array_multisort($arrColumn, $reverse ? SORT_DESC : SORT_ASC, $array);
}
/**
* 添加索引
* @param array $array
* @param $key
* @return array
*/
public static function index (array $array, $key)
{
$ret = [];
foreach ( $array as $val ) {
$ret[$val[$key]] = $val;
}
return $ret;
}
/**
* 添加前後綴
* @param $array
* @param null $pre
* @param null $suf
* @return array
*/
public static function addFix (array $array, $pre = NULL, $suf = NULL)
{
$ret = [];
foreach ( $array as $key => $val ) {
$ret[$key] = $pre.$val.$suf;
}
return $ret;
}
}
數據檢查類
class libCheck
{
/**
* 是否為IP(IPv4)
* @param $ip
* @return bool
*/
public static function isIP ($ip)
{
$isIP = filter_var($ip, FILTER_VALIDATE_IP) ? TRUE : FALSE;
return $isIP;
}
/**
* 是否為郵箱地址
* @param $mail
* @return bool
*/
public static function isMail ($mail)
{
$isMail = filter_var($mail, FILTER_VALIDATE_EMAIL) ? TRUE : FALSE;
return $isMail;
}
/**
* 是否為URL
* @param $url
* @return bool
*/
public static function isURL ($url)
{
$isMail = filter_var($url, FILTER_VALIDATE_URL) ? TRUE : FALSE;
return $isMail;
}
/**
* 是否為正整數
* @param $num
* @return bool
*/
public static function isPosiInt ($num)
{
$isPosiInt = is_int($num) && $num > 0;
return $isPosiInt;
}
/**
* 是否在范圍內
* @param $num
* @param int $min
* @param null $max
* @return bool
*/
public static function isBetween ($num, $min = 0, $max = NULL)
{
$isBetween = $num >= $min;
if ( $max ) {
$isBetween = $isBetween && $num <= $max;
}
return $isBetween;
}
/**
* 是否為有效長度
* @param $str
* @param int $min
* @param null $max
* @return bool
*/
public static function isValidLen ($str, $min = 1, $max = NULL)
{
$length = mb_strlen($str);
$isValidLen = self::isBetween($length, $min, $max);
return $isValidLen;
}
}
文件類
class libFile
{
/**
* 獲取目錄下所有文件
* @param $path
* @return array
*/
public static function getFiles ($path)
{
$path = self::formatePath($path);
$dir = opendir($path);
$files = [];
while ( ($filename = readdir($dir)) !== FALSE ) {
if ( $filename != '.' && $filename != '..' ) {
$files[] = $path.$filename;
}
}
closedir($dir);
return $files;
}
/**
* 按模式取目錄下文件
* @param $path
* @param $pattern
* @return array
*/
public static function getFilesByPattern ($path, $pattern)
{
$path = self::formatePath($path);
$filePattern = sprintf('%s%s', $path, $pattern);
return glob($filePattern);
}
/**
* 獲取最後修改時間
* @param $path
* @return int
*/
public static function getModifiedTime ($path)
{
return filemtime($path);
}
/**
* 格式化路徑
* @param $path
* @return string
*/
public static function formatePath ($path)
{
return rtrim($path, '/').'/';
}
}
HTTP類
class libHttp
{
const TIMEOUT_DEFAULT = 30;
private $_ch = NULL;
/**
* GET方法
* @param $url
* @param array $options
* @return bool|mixed
*/
public function get ($url, $options = [])
{
return $this->_send($url, NULL, NULL, $options);
}
/**
* POST方法
* @param $url
* @param array $params
* @param array $options
* @return bool|mixed
*/
public function post ($url, $params = [], $options = [])
{
return $this->_send($url, 'POST', $params, $options);
}
/**
* 錯誤信息
* @return string
*/
public function error ()
{
return curl_error($this->_ch);
}
/**
* 錯誤號
* @return int
*/
public function errno ()
{
return curl_errno($this->_ch);
}
/**
* @param $url
* @param string $method
* @param array $params
* @param array $options
* @return bool|mixed
*/
private function _send ($url, $method = 'GET', $params = [], $options = [])
{
$this->_ch = curl_init($url);
curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($this->_ch, CURLOPT_CONNECTTIMEOUT, self::TIMEOUT_DEFAULT);
if ( $method == 'POST' ) {
$options[CURLOPT_POST] = TRUE;
$options[CURLOPT_POSTFIELDS] = $params;
}
curl_setopt_array($this->_ch, $options);
$result = curl_exec($this->_ch);
return ($this->errno() == 0) ? $result : FALSE;
}
/**
* __destruct
*/
public function __destruct ()
{
curl_close($this->_ch);
}
}
字符串類
class libString
{
/**
* 隨機字符串
* @param int $length
* @param bool $num
* @param bool $lower
* @param bool $upper
* @return string
*/
public static function rand ($length=5, $num=TRUE, $lower=TRUE, $upper=TRUE)
{
$str = '';
$num && $str .= '0123456789';
$lowercase && $str .= 'abcdefghijklmnopqrstuvwxyz';
$uppercase && $str .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$rand = substr(str_shuffle($str), -$length);
return $rand;
}
/**
* 字符串截取(單次)
* @param $ld
* @param $rd
* @param $str
* @return string
*/
public static function sub ($ld, $rd, $str)
{
$start = strpos($str, $ld) + strlen($ld);
$end = strpos($str, $rd, $start);
$data = substr($str, $start, $end - $start);
return $data;
}
/**
* 字符串截取(批量)
* @param $ld
* @param $rd
* @param $str
* @return array
*/
public static function subs ($ld, $rd, $str)
{
$data = [];
$lLen = strlen($ld);
$rLen = strlen($rd);
$offset = 0;
while ( ($start = strpos($str, $ld, $offset)) !== FALSE ) {
$start += $lLen;
$end = strpos($str, $rd, $start);
$data[] = substr($str, $start, $end - $start);
$offset = $end + $rLen;
}
return $data;
}
/**
* 生成唯一ID
* @return string
*/
public static function uniqid ()
{
return md5(uniqid(rand(), true));
}
}
其它工具類
class libTool
{
/**
* ip->int
* @param $ip
* @return string
*/
public static function ip2long ($ip)
{
$long = sprintf('%u', ip2long($ip));
return $long;
}
/**
* 系統負載
* @return mixed
*/
public static function loadAvg ()
{
$load = sys_getloadavg()[0];
return $load;
}
/**
* 寫日志文件
* @param $file
* @param $msg
* @param bool $newLine
*/
public static function log ($file, $msg, $newLine = TRUE)
{
$newLine && $msg .= "\n";
file_put_contents($file, $msg, FILE_APPEND);
}
/**
* 內存使用量
* @return float
*/
public static function memoryUsage ()
{
$memery = memory_get_usage() / 1024 / 1024;
return $memery;
}
/**
* 浏覽器變量輸出
* @param $var
* @param bool|FALSE $return
* @param bool|TRUE $strict
* @return bool|mixed|string
*/
public static function dump ($var, $return = FALSE, $strict = TRUE)
{
if (!$strict) {
if (ini_get('html_errors')) {
$output = print_r($var, TRUE);
$output = sprintf('<pre>%s</pre>', htmlspecialchars($output, ENT_QUOTES));
} else {
$output = print_r($var, TRUE);
}
} else {
ob_start();
var_dump($var);
$output = ob_get_clean();
if (!extension_loaded('xdebug')) {
$output = preg_replace('/\]\=\>\n(\s+)/m', '] => ', $output);
$output = sprintf('<pre>%s</pre>', htmlspecialchars($output, ENT_QUOTES));
}
}
if ($return) {
return $output;
} else {
echo $output;
return TRUE;
}
}
}