分割汉字
$str = "你好啊";
preg_match_all("/./u", $str, $arr);
print_r($arr[0]);
手机号码中间四位替换
function mobile_replace($mobile)
{
if (strlen($mobile) !== 11) {
return $mobile;
}
return substr_replace($mobile, '****', 3, 4);
}
生成4位UUID
function create_uuid()
{
$str = base64_encode(openssl_random_pseudo_bytes(3));
$reg = "/\/|\~|\!|\@|\#|\\$|\%|\^|\&|\*|\(|\)|\(|\)|\_|\+|\{|\}|\:|\<|\>|\?|\[|\]|\,|\.|\/|\;|\'|\`|\-|\=|\\\|\||\s+/";
return !preg_match($reg, $str) ? $str : create_uuid();
}
从其他网站扒数据
function http_curl($url,$post=[]){
$post = http_build_query($post);
$ch = curl_init ();
curl_setopt ($ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt ( $ch,
CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch,
CURLOPT_POSTFIELDS, $post );
$return = curl_exec ( $ch );
if(curl_errno($ch)){
die(curl_error($ch));
}
curl_close ( $ch );
return $return;
}
数组写入文本文件
file_put_contents ( dirname ( __FILE__ )."/log.txt", date ( "Y-m-d H:i:s" ) . " " . var_export($_GET,true) . "\r\n", FILE_APPEND );
订单号生成
$yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
$order['sn'] = $yCode[rand(0,9)] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%06d', rand(0,999999));
加密函数 E为加密 D为解密
function encrypt2($string, $operation = 'D', $key = 'ZitYyk8IAffzJVuNmzMYnugVww') {
$key = md5($key);
$key_length = strlen($key);
$string = $operation == 'D' ? base64_decode($string) : substr(md5($string . $key), 0, 8) . $string;
$string_length = strlen($string);
$rndkey = $box = array();
$result = '';
for ($i = 0; $i <= 255; $i++) {
$rndkey[$i] = ord($key[$i % $key_length]);
$box[$i] = $i;
}
for ($j = $i = 0; $i < 256; $i++) {
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}
for ($a = $j = $i = 0; $i < $string_length; $i++) {
$a = ($a + 1) % 256;
$j = ($j + $box[$a]) % 256;
$tmp = $box[$a];
$box[$a] = $box[$j];
$box[$j] = $tmp;
$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
}
if ($operation == 'E') {
//解密
if (substr($result, 0, 8) == substr(md5(substr($result, 8) . $key), 0, 8)) {
$ret = substr($result, 8);
if (strlen($ret) == 11) {
if (session('member.groupid') == 1 || session('member.groupid') == 3) {
return $ret;
} else {
$pattern = "/(\d{3})\d{4}(\d{4})/";
$replacement = "\$1****\$2";
return preg_replace($pattern, $replacement, $ret);
}
} else {
return $ret;
}
} else {
return '';
}
} else {
return str_replace('=', '', base64_encode($result));
}
}
访问人数
$ip= $_SERVER['REMOTE_ADDR'];
if($_SESSION[$ip] != 1){
M('User_address')->where("id=1")->setInc('ip');
$_SESSION[$ip] = 1;
}
在线人数
$filename='online.txt';//数据文件
$cookiename='VGOTCN_OnLineCount';//cookie名称
$onlinetime=600;//在线有效时间,单位:秒 (即600等于10分钟)
$online=file($filename); //PHP file() 函数把整个文件读入一个数组中。与 file_get_contents() 类似,不同的是 file() 将文件作为一个数组返回。数组中的每个单元都是文件中相应的一行,包括换行符在内。如果失败,则返回 false
$nowtime=$_SERVER['REQUEST_TIME'];
$nowonline=array(); //得到仍然有效的数据
foreach($online as $line){
$row=explode('|',$line);
$sesstime=trim($row[1]);
if(($nowtime - $sesstime)<=$onlinetime){//如果仍在有效时间内,则数据继续保存,否则被放弃不再统计
$nowonline[$row[0]]=$sesstime;//获取在线列表到数组,会话ID为键名,最后通信时间为键值
}
} /* @创建访问者通信状态 使用cookie通信 COOKIE 将在关闭浏览器时失效,但如果不关闭浏览器,此 COOKIE 将一直有效,直到程序设置的在线时间超时 */
if(isset($_COOKIE[$cookiename])){//如果有COOKIE即并非初次访问则不添加人数并更新通信时间
$uid=$_COOKIE[$cookiename];
}else{//如果没有COOKIE即是初次访问
$vid=0;//初始化访问者ID
do{//给用户一个新ID
$vid++;
$uid='U'.$vid;
}while(
array_key_exists($uid,$nowonline));
setcookie($cookiename,$uid);
}
$nowonline[$uid]=$nowtime;//更新现在的时间状态 //统计现在在线人数
$total_online=count($nowonline); //写入数据
if($fp=@fopen($filename,'w')){
if(flock($fp,LOCK_EX)){
rewind($fp);
foreach($nowonline as $fuid=>$ftime){
$fline=$fuid.'|'.$ftime."\n";
@fputs($fp,$fline); }
flock($fp,LOCK_UN);
fclose($fp); } }
$this->assign('online',$total_online);
清除BOM头(复制保存为PHP文件放到网站根目录下运行)
<?php
/**
* 用法:复制以下代码至新建的php文件中,将该php文件放置项目目录,运行即可。代码来源于网络。
* chenwei 注。
*/
header('content-Type: text/html; charset=utf-8');
$auto=1;/* 设置为1标示检测BOM并去除,设置为0标示只进行BOM检测,不去除 */
$basedir='.';
$loop=true;
echo '当前查找的目录为:'.$basedir.'当前的设置是:';
echo '(1)',$loop?'检查当前目录以及当前目录的子目录':'只针对当前目录进行检测';
echo '(2)',$auto?'检测文件BOM同时去除检测到BOM文件的BOM<br />':'只检测文件BOM不执行去除BOM操作<br />';
checkdir($basedir,$loop);
function checkdir($basedir='',$loop=true){
$basedir=empty($basedir)?'.':$basedir;
if($dh=opendir($basedir)){
while (($file=readdir($dh))!==false){
if($file!='.'&&$file!='..'){
if(!is_dir($basedir.'/'.$file)){
echo '文件: '.$basedir.'/'.$file .checkBOM($basedir.'/'.$file).' <br>';
}else{
if(!$loop) continue;
$dirname=$basedir.'/'.$file;
checkdir($dirname);
}
}
}
closedir($dh);
}
}
function checkBOM($filename){
global $auto;
$contents=file_get_contents($filename);
$charset[1]=substr($contents,0,1);
$charset[2]=substr($contents,1,1);
$charset[3]=substr($contents,2,1);
if(ord($charset[1])==239&&ord($charset[2])==187&&ord($charset[3])==191){
if($auto==1){
$rest=substr($contents,3);
rewrite($filename,$rest);
return (' <font color=red>找到BOM并已自动去除</font>');
}else{
return (' <font color=red>找到BOM</font>');
}
}else{
return (' 没有找到BOM');
}
}
function rewrite($filename,$data){
$filenum=fopen($filename,'w');
flock($filenum,LOCK_EX);
fwrite($filenum,$data);
fclose($filenum);
}
导出表格
//php文件
public function exportExcel(){
//php中$_REQUEST可以获取以POST方法和GET方法提交的数据,但是速度比较慢
$arr_id = $_REQUEST['ids'];
if($arr_id==null)
{
echo "<script language=\"JavaScript\">";
echo "alert(\"请选择你要导出的数据\");";
echo "history.go(-1);";
echo "</script>";
return false;
// echo '<span><a href="javascript:history.go(-1);"></a></span>';
}
else{
$where['id'] = array('in', $arr_id);
//导出表中数据
$Information=M("user")->where($where)->getField('username,email,mobile,banji');
foreach($Information as $key=>$val){
// $Information[$key]['riqi'] = date("Y-m-d H:i:s",$val['riqi']);
}
$title = array("用户名",'邮箱','电话','所在班级');
$this->exportexcelneibu($Information,$title);
}
}
/**
* 导出数据为excel表格
*@param $data 一个二维数组,结构如同从数据库查出来的数组
*@param $title excel的第一行标题,一个数组,如果为空则没有标题
*@param $filename 下载的文件名
*@examlpe
*$stu = M ('User');
*$arr = $stu -> select();
*exportexcel($arr,array('id','账户','密码','昵称'),'文件名!');
*/
private function exportexcelneibu($data=array(),$title=array(),$filename='注册用户信息'){
header("Content-type:application/octet-stream");
header("Accept-Ranges:bytes");
header("Content-type:application/vnd.ms-excel");
//文件后缀可以随便换,各种格式的都可以
header("Content-Disposition:attachment;filename=".$filename.".xls");
header("Pragma: no-cache");
header("Expires: 0");
//导出xls 开始
if (!empty($title)){
foreach ($title as $k => $v) {
$title[$k]=iconv("UTF-8", "GB2312",$v);
}
$title= implode("\t", $title);
echo "$title\n";
}
if (!empty($data)){
foreach($data as $key=>$val){
foreach ($val as $ck => $cv) {
$data[$key][$ck]=iconv("UTF-8", "GB2312", $cv);
}
$data[$key]=implode("\t", $data[$key]);
}
echo implode("\n",$data);
}
}
//静态页面
<input type="submit" class="button" name="dosubmit" value="<?php echo '导出excle表格'?>" onclick="myform.action='{:U($module_name.'/exportExcel')}';$('#myform').submit();" />
发送短信(【阿里短信服务】
//https://market.aliyun.com/products/57002003/cmapi011900.html?spm=5176.2020520132.101.7.ZFc96f)
public function sendsms(){
$host = "http://sms.market.alicloudapi.com";
$path = "/singleSendSms";
$method = "GET";
$appcode = "";
$headers = array();
$person = $_POST['mobile'];
$code = rand(0,999999);
$_SESSION['smsVsrify']= md5($code);
$code = array("code"=>"$code");
$codeMd = json_encode($code);
$sign = urlencode('工程管理平台');
array_push($headers, "Authorization:APPCODE " . $appcode);
$querys = "ParamString=".$codeMd."&RecNum=".$person."&SignName=".$sign."&TemplateCode=SMS_000001";
$bodys = "";
$url = $host . $path . "?" . $querys;
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
if (1 == strpos("$".$host, "https://"))
{
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
curl_exec($curl);
echo 1;
}
生成毫秒(年月日、时分秒 + 3位毫秒数)
//ts_time('YmdHisu')
function ts_time($format = 'u', $utimestamp = null) {
if (is_null($utimestamp)){
$utimestamp = microtime(true);
}
$timestamp = floor($utimestamp);
$milliseconds = round(($utimestamp - $timestamp) * 1000);
return date(preg_replace('`(?<!\\\\)u`', $milliseconds, $format), $timestamp);
}