这篇文章介绍的内容是关于php的常用函数,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
<?php
/**
* @strhidtel 字符串打码
* ===========================
* @str 字符串
* @percen 百分比
* @count 次数
* @tostr 替换的字符串
*/
function strhidtel($str, $percen = 30, $count = 1, $tostr = "0")
{
// 判断字符串长度
$strlen = mb_strlen($str, "utf-8");
// 计算中间百分比值
$maxlen = ceil($strlen * $percen / 100);
$startlen = $strlen - $maxlen;
// 生成同等长度的*
$blurry = str_repeat($tostr, $maxlen);
// 截取要替换的字符串
$shiftext = mb_substr($str, $startlen / 2, $maxlen, "utf-8");
// 替换字符串
return str_replace($shiftext, $blurry, $str, $count);
}
/** * @savelog 存储日志
* =========================
* @text 储存内容
* @type 类型
*/
function savelog ($type, $text)
{
// 初始化
date_default_timezone_set("prc");
$day=date("y-m-d",time());
$time = "[".date("y-m-d h:i:s",time())."]";
$hade="[".$_server['request_uri']."]";
$logpath = './';
$filepath = $logpath.$type.$day.".log";
// 判断文件是否存在
$f = fopen ($filepath, "ab+");
// 写入
fwrite($f, "$time-$hade-$text"."\r\n");
fclose($f);}/** * @getip 获取客户端ip
*/
function getip()
{
if (getenv('http_client_ip'))
{
$ip = getenv('http_client_ip');
}
elseif (getenv('http_x_forwarded_for'))
{
$ip = getenv('http_x_forwarded_for');
}
elseif (getenv('http_x_forwarded'))
{
$ip = getenv('http_x_forwarded');
}
elseif (getenv('http_forwarded_for'))
{
$ip = getenv('http_forwarded_for');
}
elseif (getenv('http_forwarded'))
{
$ip = getenv('http_forwarded');
}
else
{
$ip = $_server['remote_addr'];
}
preg_match("/^(\d|\.)+$/si", $ip, $isip);
if($isip == null)
{
f404("系统无法验证您的身份:$ip");
exit;
}
return $ip;
}
/**
* @intip 获取客户端的int型ip
*/
function intip()
{
// 转换为数字ip
$intip = ip2long(getip());
// 判断是否转换失败
if ($intip) {
return $intip;
} else {
return false;
}}
/**
* @ordergenerate 订单号生成
* ===========================
* @userid 用户id
* @type 订单类型 1:充值, 2:提现, 或获取具体类型
* @id 相关id
*/
function ordergenerate($userid, $type, $id = "0")
{
// 初始化
$time = time();
$ip = str_pad(abs(intip()), 10, "0");
// 生成随机数3位
$randsub = randomstr(3, 1);
// 填补用户id
$usertext = str_pad($userid, 10, "0", str_pad_left);
// 初始化8
$orderid = date("ymd", $time);
// 截取最后一位相关id9
$orderid .= substr($id, -1);
// 填补类型id14
$orderid .= str_pad($type, 5, "0");
// 填充随机数17
$orderid .= $randsub;
// 填充ip前半段19
$orderid .= mb_substr($ip, 3, 2);
// 填补时间戳后5位24
$orderid .= mb_substr($time, 4, 5);
// 填充ip前后段29
$orderid .= mb_substr($ip, 7, 3);
// 截取用户后4位id31
$orderid .= mb_substr($usertext, 6, 4);
// 输出
return $orderid;
}
/**
* @isstring 判断变量是否存在且为字符串
* =======================================
* @variable 实际传入变量 array
* @keyname 键名 array
* @exclude 排除
*/
function isstring ($isstring, $keyname, $exclude = array())
{
// 判断条数是否相等
if((is_array($isstring) && is_array($keyname) && count($isstring) == count($keyname))
|| (count($isstring) == (count($keyname) + count($exclude))))
{
// 循环实际传入变量
foreach($keyname as $list => $as)
{
// 判断是否存在于传入之中,且为字符串
if(!(isset($isstring[$as]) && (is_string($isstring[$as]) || is_int($isstring[$as]))))
{ if (!in_array($as, $exclude))
{ return false;
}
}
}
// 无误输出
return true;
} else {
return false;
}
}
$date=['a'=>123,'b'=>456,'c'=>789,'d'=>'<script>alert(111111)</script>'];
/**
* @isutf8 编码转换
* @str 传入字符串
*/
function isutf8($str)
{
$encode = mb_detect_encoding($str, array(ascii,'utf-8',gb2312,gbk,big5));
if($encode != 'utf-8') {
$str = iconv($encode,utf-8,$str);; }
return $str;}
/********************php验证身份证号码是否正确函数*********************/
function is_idcard( $id ){ $id = strtoupper($id);
$regx = /(^\d{15}$)|(^\d{17}([0-9]|x)$)/; $arr_split = array();
if(!preg_match($regx, $id)) { return false; }
if(15==strlen($id)) //检查15位 { $regx = /^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/;
@preg_match($regx, $id, $arr_split);
//检查生日日期是否正确
$dtm_birth = 19.$arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4]; if(!strtotime($dtm_birth)) {
return false; } else { return true; } } else
//检查18位
{
$regx = /^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|x)$/; @preg_match($regx, $id, $arr_split);
$dtm_birth = $arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];
if(!strtotime($dtm_birth)) //检查生日日期是否正确 { return false; } else {
//检验18位身份证的校验码是否正确。
//校验位按照iso 7064:1983.mod 11-2的规定生成,x可以认为是数字10。
$arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
$arr_ch = array('1', '0', 'x', '9', '8', '7', '6', '5', '4', '3', '2');
$sign = 0; for ( $i = 0; $i < 17; $i++ ) {
$b = (int) $id{$i}; $w = $arr_int[$i]; $sign += $b * $w; }
$n = $sign % 11; $val_num = $arr_ch[$n];
if ($val_num != substr($id,17, 1)) { return false; } //phpfensi.com
else { return true; } } }}
相关推荐:
php函数的学习笔记
以上就是php的常用函数的详细内容。