菜鸡源码,专注精品下载!
当前位置:首页 > 建站教程 > 建站知识

PHP常用函数大全:全面解析

发布时间:2024-01-05  栏目:建站知识   浏览:   分类:php教程 PHP函数

PHP常用函数大全包括了许多实用的函数,如字符串处理函数、数学函数、日期和时间函数等。例如,strlen()函数用于获取字符串的长度,substr_count()函数用于计算子字符串在字符串中出现的次数,intval()函数用于将字符串转换为整数,floatval()函数用于将字符串转换为浮点数,date()函数用于获取当前日期和时间,time()函数用于获取当前时间戳等。这些函数可以帮助我们更方便地处理数据和编写代码。

PHP随机图片API本地图片版,页面直接输出图片

<?php$img_array=glob('images/*.{gif,jpg,png,jpeg,webp,bmp}',GLOB_BRACE);if(count($img_array)==0)die('没找到图片文件。请先上传一些图片到'.dirname(__FILE__).'/images/文件夹');header('Content-Type:image/png');echo(file_get_contents($img_array[array_rand($img_array)]));?>

将图片保存到images目录下,自动读取images图片并输出。

PHP随机图片API远程图片版,页面直接输出图片

<?php$imgku=file('pic.txt');showImg($imgku[array_rand($imgku)]);/**php页面直接输出图片*/functionshowImg($img){$img=trim($img);$info=getimagesize($img);$imgExt=image_type_to_extension($info[2],false);//获取文件后缀$fun="imagecreatefrom{$imgExt}";$imgInfo=$fun($img);//1.由文件或URL创建一个新图象。如:imagecreatefrompng(string$filename)//$mime=$info['mime'];$mime=image_type_to_mime_type(exif_imagetype($img));//获取图片的MIME类型header('Content-Type:'.$mime);$quality=100;if($imgExt=='png')$quality=9;//输出质量,JPEG格式(0-100),PNG格式(0-9)$getImgInfo="image{$imgExt}";$getImgInfo($imgInfo,null,$quality);//2.将图像输出到浏览器或文件。如:imagepng(resource$image)imagedestroy($imgInfo);}?>

需安装GD库及exif扩展,php.ini中开启allow_url_fopen函数,读取同目录下pic.txt文件中的图片网址,每行一个图片地址。

Cookie限制时间再次提交

if(isset($_COOKIE['submission_time'])){$submissionTime=$_COOKIE['submission_time'];$currentTime=time();$timePassed=($currentTime-$submissionTime)/60*60;if($timePassed<24){echo"<divclass='alertalert-warning'>Youcanrecordthesalesafter24hours!Pleasewait..</div>";die();}}else{$cookieName='submission_time';$cokkieValue=time();setcookie($cookieName,$cokkieValue,time()+(+60*60*24*30),"/");}

判断字符串是否含有某分割符,若包含分割符,分割后输出全部分割后的值

if(strpos($qcont,',')===false){echo"不包含,分割字段";}else{echo"包含,分割字段,下面进行切割并输出";$qcontArr=explode(",",$qcont);$qcontcount=count($qcontArr);for($i=0;$i<$qcontcount;$i++){if($qcontArr[$i]==""){continue;}echo$qcontArr[$i];}}

对错误的详情进行格式化输出,记入log文件。

functionslog($logs){$toppath="log.htm";$Ts=fopen($toppath,"a+");fputs($Ts,$logs."rn");fclose($Ts);}

使用file_get_contents() 发送GET、post请求

1、【GET请求】

$data=array('name'=>'zhezhao','age'=>'23');$query=http_build_query($data);$url='http://localhost/get.php';//这里一定要写完整的服务页面地址,否则php程序不会运行$result=file_get_contents($url.'?'.$query);

2、【POST请求】

$data=array('user'=>'jiaxiaozi','passwd'=>'123456');$requestBody=http_build_query($data);$context=stream_context_create(['http'=>['method'=>'POST','header'=>"Content-Type:application/x-www-form-urlencodedrn"."Content-Length:".mb_strlen($requestBody),'content'=>$requestBody]]);$response=file_get_contents('http://server.test.net/login',false,$context);

php获取当天是几号、周几

echodate('y').'</br>';//当前年份echodate('m').'</br>';//当前月份echodate('d').'</br>';//当前日echodate('s').'</br>';//当前秒echodate('w').'</br>';//当前周几

打印结果显示为:

200724505

PHP给第三方接口POST或GET方式传输数据并得到返回值

functionPost($url,$post=null){$context=array();if(is_array($post)){ksort($post);$context['http']=array('timeout'=>60,'method'=>'POST','content'=>http_build_query($post,'','&'),);}returnfile_get_contents($url,false,stream_context_create($context));}$data=array('name'=>'test','email'=>'test@gmail.com','submit'=>'submit',);echoPost('http://www.baidu.com',$data);

同一页面24小时之内之只能执行一次

define('TIME_OUT',86400);//定义重复操作最短的允许时间,单位秒@session_start();$time=time();if(isset($_SESSION['time'])){if($time-$_SESSION['time']<=TIME_OUT){echo'<scripttype=text/javascript>alert("在24小时内只能执行一次!");</script>';exit();}}$_SESSION['time']=$time;echo"正常执行!";

PHP连接远程MSSQL函数:

已在如上环境安装后测试通过!

functionmssql_user($username){$host="远程服务器IP,MSSQL端口";$dbname="数据库名称";$user="数据库用户名";$pass="数据库密码";try{$dbh=newPDO("sqlsrv:Server=$host;Database=$dbname",$user,$pass);}catch(PDOException$e){echo$e->getMessage();exit;}$stmt=$dbh->prepare("SELECTXXXFROMXXXWHEREXXX=".$username);$stmt->execute();while($row=$stmt->fetch()){echo$row[0];//多个查询结果输出//return$row[0];单一的结果可以直接用return}unset($dbh);unset($stmt);}

php时间戳和日期相互转换

获取当前日期时间的时间戳

echotime();

获取当前日期时间

echodate("Y/m/dH:i:s");

日期转换为时间戳

echostrtotime(date("Y/m/d"));

时间戳转换为日期

echodate('Y-m-d',time());

打印明天此时的时间戳

echostrtotime("+1day");

当前时间:

echodate("Y-m-dH:i:s",time());

指定时间:

echodate("Y-m-dH:i:s",strtotime("+1day"));

下个星期此时的时间戳

echostrtotime("+1week");

指定下星期几的PHP时间戳

echostrtotime("nextThursday");

指定下星期几的时间:

echodate("Y-m-dH:i:s",strtotime("nextThursday"));

指定上星期几的时间戳

echostrtotime("lastThursday");

指定本年的最后一个星期几的时间:

echodate("Y-m-dH:i:s",strtotime("lastThursday"));

截取指定两个字符之间的字符串

方法一

functioncut($begin,$end,$str){$b=mb_strpos($str,$begin)+mb_strlen($begin);$e=mb_strpos($str,$end)-$b;returnmb_substr($str,$b,$e);}

方法二

functionget_between($input,$start,$end){$substr=substr($input,strlen($start)+strpos($input,$start),(strlen($input)-strpos($input,$end))*(-1));return$substr;}

方法一当截取的是值为串的时候,会出现截取不到的情况用方法二尝试。

方法三:preg_match_all函数

preg_match_all('/<Epoch>(.*)</Epoch>/',$result,$matches);//print_r($matches);$resultapp=$matches[1][1];

方法一及方法二在截取长段字符串时,碰到过无法截取到的情况,用方法三解决。

调用SOHU API获取IP地址

//通过API获取IP地址functiongetIP(){$str=file_get_contents('https://pv.sohu.com/cityjson?ie=utf-8');$ip=cut('cip":"','","cid',$str);if($ip){return$ip;}}

注:需配合上面 截取指定两个字符之间的字符串 函数一起使用

获取访问客户端的IP地址

functionget_client_ip(){static$realip;if(isset($_SERVER)){if(isset($_SERVER["HTTP_X_FORWARDED_FOR"])){$realip=$_SERVER["HTTP_X_FORWARDED_FOR"];}elseif(isset($_SERVER["HTTP_CLIENT_IP"])){$realip=$_SERVER["HTTP_CLIENT_IP"];}else{$realip=$_SERVER["REMOTE_ADDR"];}}else{if(getenv("HTTP_X_FORWARDED_FOR")){$realip=getenv("HTTP_X_FORWARDED_FOR");}elseif(getenv("HTTP_CLIENT_IP")){$realip=getenv("HTTP_CLIENT_IP");}else{$realip=getenv("REMOTE_ADDR");}}return$realip;}


相关文章

    无相关信息
评论
建站知识
建站知识
使用技巧
调试安装
运营推广