<?php
/**
* 检查数组是否全是空值
*/
function isEmptyArray($arr) {
if(is_array($arr)) {
foreach($arr as $v) {
if(isEmptyArray($v) == false) {
return false;
}
}
} else {
return empty($arr);
}
return true;
}
/**
* 去除头尾空格和转义
*/
function escape($str,$as = true) {
if(is_array($str)) {
foreach($str as $key=>$val) {
$str[$key] = $this->escape($val,$as);
}
} else {
if($as) {
$str = addslashes(trim($str));
} else {
$str = trim($str);
}
}
return $str;
}
/**
* 判断是否有中文字符
*/
function isChinese($word) {
return preg_match ("/[\x{4e00}-\x{9fa5}]+/u", $word);
}
/**
* 判断是否是只有中文文字
*/
function isCn($word) {
return preg_match("/^[\x{4e00}-\x{9fa5}]+$/u", $word);
}
/**
* 将汉字字符串处理为数组
*/
function cn_split($str) {
return preg_split('/(?<!^)(?!$)/u', $str);
}
/**
* 用以代替file_get_contents
* 需要cURL支持,当连接超时则返回false,不会卡死php页面
*/
function an_get_contents($url, $second = 5) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_TIMEOUT,$second);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
/**
* 转换至UTF-8编码
* 适用于大部分中文情况
*/
function convertToUTF8($str) {
$charset = mb_detect_encoding($str, array('ASCII','UTF-8','GB2312','GBK','BIG5'));
if ($charset!='UTF-8') {
$str = mb_convert_encoding($str,'UTF-8',$charset);
}
return $str
}
/**
* 生成指定范围内指定个数的不重复随机数
*/
function getRand($min, $max, $num) {
if($max-$min<$num) return false;
$res = null;
$arr = range($min,$max);
shuffle($arr);
$rand_keys = array_rand($arr, $num);
foreach($rand_keys as $v) {
$res[] = $arr[$v];
}
return $res;
}
function add_page_css($s) {
$rs = '';
if(!is_array($s)) {
$rs = '<link href="res/css/'.$s.'.css" rel="stylesheet" type="text/css" />';
} else {
foreach($s as $v) {
$rs .= '<link href="res/css/'.$v.'.css" rel="stylesheet" type="text/css" />';
}
}
return $rs;
}
function add_page_js($s) {
$rs = '';
if(!is_array($s)) {
$rs = '<script type="text/javascript" src="res/js/'.$s.'.js"></script>';
} else {
foreach($s as $v) {
$rs .= '<script type="text/javascript" src="res/js/'.$v.'.js"></script>';
}
}
return $rs;
}
?>