1、生成n位编号,前面用0填充
sprintf('%05s', 1);
// 结果 00001
2、生成树状图
/**
* 获得树状数组
*/
public static function listToTree($list, $primaryKey='id', $parentKey = 'pid', $childStr = 'children', $root = 0 ,array &$tree)
{
if (is_array($list)) {
//创建基于主键的数组引用
$refer = array();
foreach ($list as $key => $data) {
$refer[$data[$primaryKey]] = &$list[$key];
}
foreach ($list as $key => $data) {
//判断是否存在parent
$parantId = $data[$parentKey];
if ($root == $parantId) {
$tree[] = &$list[$key];
} else {
if (isset($refer[$parantId])) {
$parent = &$refer[$parantId];
$parent[$childStr][] = &$list[$key];
}
}
}
}
return $tree;
}
3、截取n个字,支持中英文
/*
Utf-8、gb2312都支持的汉字截取函数
cut_str(字符串, 截取长度, 开始长度, 编码);
编码默认为 utf-8
开始长度默认为 0
*/
function cutStr($string, $sublen, $start = 0, $code = 'UTF-8'){
if($code == 'UTF-8'){
$pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
preg_match_all($pa, $string, $t_string);
if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."...";
return join('', array_slice($t_string[0], $start, $sublen));
}else{
$start = $start*2;
$sublen = $sublen*2;
$strlen = strlen($string);
$tmpstr = '';
for($i=0; $i<$strlen; $i++){
if($i>=$start && $i<($start+$sublen)){
if(ord(substr($string, $i, 1))>129){
$tmpstr.= substr($string, $i, 2);
}else{
$tmpstr.= substr($string, $i, 1);
}
}
if(ord(substr($string, $i, 1))>129) $i++;
}
if(strlen($tmpstr)<$strlen ) $tmpstr.= "...";
return $tmpstr;
}
}
4、手机验证
/**
* Determine if the validation rule passes.
*
* 移动号段: 13[4-9], 147,148, 15[0-2,7-9],165, 170[3,5,6],172,178, 18[2-4,7-8], 19[5,7,8]
* 联通号段: 130,131,132,145,146, 155,156, 166,167,170[4,7,8,9],171,175,176, 185,186, 196
* 电信号段: 133, 149, 153, 162, 170[0,1,2],173,174[0-5],177,180,181,189, 19[0,1,3,9]
* 广电号段: 192
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$this->value = $value;
$reg = '/^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][2,5,6,7])|([7][0-8])|([8][0-9])|([9][0-3,5-9]))[0-9]{8}$/';
if( preg_match($reg, $value)) {
return true;
}
return false;
}
5、公共curl函数
/**
* @param $URL
* @param $type
* @param $params
* @param $headers
* @return mixed
*/
static public function callInterfaceCommon($URL, $type, $params, $headers)
{
try {
$ch = curl_init();
$timeout = 20;
curl_setopt($ch, CURLOPT_URL, $URL); //接口地址
if ($headers != "") {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
} else {
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type:application/json;charset=utf-8"));
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
switch ($type) {
case "GET" :
curl_setopt($ch, CURLOPT_HTTPGET, true);
break;
case "POST":
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
break;
case "PUT" :
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
break;
case "DELETE":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
break;
}
$file_contents = curl_exec($ch);//获得返回值
//获取错误编码
$curlErrno = curl_errno($ch);
if ($curlErrno) {
throw new \Exception(curl_error($ch) . '(' . $curlErrno . ')');
}
curl_close($ch);
$result = json_decode($file_contents, true);
$result['curl_status'] = 1;
$result['curl_message'] = '';
} catch (\Exception $e) {
$result['curl_status'] = 0;
$result['curl_message'] = $e->getMessage();
}
return $result;
}
6、字符串超出长度用'...'表示
/**
* 格式化字符串
*
* @param [type] $txt
* @param integer $lenght
* @return void
*/
static public function formatString($txt, $lenght = 10)
{
if (mb_strlen($txt) > $lenght) {
return mb_substr($txt, 0, $lenght - 3) . '...';
} else {
return $txt;
}
}
7、手机号验证(可根据需要修改正则)
/**
* 移动号段: 13[4-9], 147,148, 15[0-2,7-9],165, 170[3,5,6],172,178, 18[2-4,7-8], 19[5,7,8]
* 联通号段: 130,131,132,145,146, 155,156, 166,167,170[4,7,8,9],171,175,176, 185,186, 196
* 电信号段: 133, 149, 153, 162, 170[0,1,2],173,174[0-5],177,180,181,189, 19[0,1,3,9]
* 广电号段: 192
*
* @param string $mobile
* @return bool
*/
public function isMobile($mobile)
{
$reg = '/^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][2,5,6,7])|([7][0-8])|([8][0-9])|([9][0-3,5-9]))[0-9]{8}$/';
if( preg_match($reg, $mobile)) {
return true;
}
return false;
}
8、获得客户端IP
/**
* 得到客户端IP
* @param int $type 返回id类型,0:返回ip,1:返回long值
* @return string
*/
static function getClientIps($type = 0)
{
$type = $type ? 1 : 0;
static $ip = NULL;
if ($ip !== NULL) return $ip[$type];
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$pos = array_search('unknown', $arr);
if (false !== $pos) unset($arr[$pos]);
$ip = trim($arr[0]);
} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
}
// IP地址合法验证
$long = sprintf("%u", ip2long($ip));
$ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
return $ip[$type];
}
网友评论