例子
代码如下 | 复制代码 |
<?php |
原理分析
很简单的一个例子我们只要使用foreach遍历数组的值之后再使用strpos($str,$v)>-1来判断数组值是不是在字符串中指定位置了,如果搜索到了就会返回大于或等于0值哦。
今天分享一段PHP中多张图片合成一张图片的代码实现,我们可以在图片合成,水印,九宫格等需求下使用到类似这样的图片合成效果,下面一起来看看例子吧。今天分享一段PHP中多张图片合成一张图片的代码实现,我们可以在图片合成,水印,九宫格等需求下使用到类似这样的图片合成效果,下面一起来看看例子吧。
例子
代码如下 | 复制代码 |
$imgs = array(); <img" width=100% src="pin.jpg"> |
下面是网上比较流行的纯PHP实现定时任务的代码,我把他整理完善了下,不过先别急着用,一定要看到最下面。
代码如下 | 复制代码 |
<?php do { |
上面的代码是死循环,即使删除代码文件也无济于事,必须重启WEB才能停止脚本执行。所以我们需要一个执行脚本的开关,我们可以用外部文件引入的方法来实现,在while循环的时候,include开关变量即可。
建立外部引入变量文件 switch.php 内容如下:
代码如下 | 复制代码 |
<?php return 1;//1执行脚本 0退出执行脚本 ?> 改良版的PHP代码 <?php ignore_user_abort(); // 关掉浏览器,PHP脚本也可以继续执行 set_time_limit(600); // 设置程序的执行时间为600秒 // set_time_limit(0); // 设置程序的执行时间为无限长 $interval = 5; // 每隔5秒运行 $switch = include 'switch.php'; while($switch){ $fp = fopen('sjyhome.txt', 'a'); // 打开 sjyhome.txt fwrite($fp,'欢迎来到SJY之家 www.111cn.net $switch \n'); // 往 sjyhome.txt 写入信息 fclose($fp); // 关闭 sjyhome.txt sleep($interval); // 等待5秒 $switch = include 'switch.php'; } exit(); ?> |
把switch.php文件中的return值改为0,则会自动停止运行。想要再次执行,必须把值改为1,并且打开浏览器再执行一次。
如果$switch值为1,那么即使不设置ignore_user_abort(),关闭浏览器照样无法停止循环,甚至删除代码文件也没用,只能等待超时(默认30秒,set_time_limit自定义执行时间)重启WEB服务。
写在后面的话
本方法纵然可以实现定时功能,可是一旦运行该代码文件,这个进程就一直都在了,对于服务器资源的消耗我没测试过,不过有些空间商或许不会允许长时间占线的进程。
如果自己有VPS或服务器的朋友,建议在服务器中做好计划任务,来执行你网站上的脚本
下面介绍使用Linux中的Crontab来实现的两种方法。
关于crontab的使用,可参考文章:Linux计划任务crontab详解
一、在Crontab中使用PHP执行脚本
就像在Crontab中调用普通的shell脚本一样(具体Crontab用法),使用PHP程序来调用PHP脚本。
每一小时执行myscript.php如下:
代码如下 | 复制代码 |
#crontab -e 00 * * * * /usr/local/bin/php /home/web/3aj.php /usr/local/bin/php为PHP程序的路径。 |
二、在Crontab中使用URL执行脚本
如果你的PHP脚本可以通过URL触发,你可以使用lynx或curl或wget来配置你的Crontab。
下面的例子是使用Lynx文本浏览器访问URL来每小时执行PHP脚本。Lynx文本浏览器默认使用对话方式打开URL。但是,像下面的,我们在lynx命令行中使用-dump选项来把URL的输出转换来标准输出。
00 * * * * lynx -dump http://你的网址/script.php
下面的例子是使用CURL访问URL来每5分执行PHP脚本。Curl默认在标准输出显示输出。使用”curl -o”选项,你也可以把脚本的输出转储到临时文件。
*/5 * * * * /usr/bin/curl -o temp.txt http://你的网址/script.php
下面的例子是使用WGET访问URL来每10分执行PHP脚本。-q选项表示安静模式。”-O temp.txt”表示输出会发送到临时文件。
*/10 * * * * /usr/bin/wget -q -O temp.txt http://你的网址/script.php
再介绍一下在Windows下php计划任务的实现
1、新建一个3aj.php,内容如下:
代码如下 | 复制代码 |
<? $fp = fopen("3aj.txt", "a+"); fwrite($fp, date("Y-m-d H:i:s") . " 成功了!\n"); fclose($fp); ?> |
2、新建3aj.bat文件,内容如下:
C:\php\php.exe -q D:\web\3aj.php
3、建立WINDOWS计划任务:
开始–>控制面板–>任务计划–>添加任务计划
浏览文件夹选择上面的3aj.bat文件
设置时间和密码(登陆WINDOWS的保存即可了。
4、右键点击计划任务 -> “运行”即可
推荐方法 CURL获取
代码如下 | 复制代码 |
<?php |
使用file_get_contents
代码如下 | 复制代码 |
<?php |
看看file_get_contents性能
1)fopen/file_get_contents 每次请求远程URL中的数据都会重新做DNS查询,并不对DNS信息进行缓存。但是CURL会自动对DNS信息进行缓存。对同一域名下的网页或者图片的请求只需要一次DNS 查询。这大大减少了DNS查询的次数。所以CURL的性能比fopen/file_get_contents 好很多。
2)fopen/file_get_contents在请求HTTP时,使用的是http_fopen_wrapper,不会keeplive。而curl却可以。这样在多次请求多个链接时,curl效率会好一些。(设置header头应该可以)
3)fopen/file_get_contents函数会受到php.ini文件中allow_url_open选项配置的影响。如果该配置关闭了,则该函数也就失效了。而curl不受该配置的影响。
4)curl可以模拟多种请求,例如:POST数据,表单提交等,用户可以按照自己的需求来定制请求。而fopen/file_get_contents只能使用get方式获取数据。
5)fopen/file_get_contents 不能正确下载二进制文件
6)fopen/file_get_contents 不能正确处理ssl请求
7)curl 可以利用多线程
8)使用 file_get_contents 的时候如果 网络出现问题, 很容易堆积一些进程在这里
9)如果是要打一个持续连接,多次请求多个页面。那么file_get_contents就会出问题。取得的内容也可能会不对。所以做一些类似采集工作的时候,肯定就有问题了。对做采集抓取的用curl,如果还有同不相信下面我们再做个测试
curl与file_get_contents性能对比PHP源代码如下:
1829.php
代码如下 | 复制代码 |
<?php /** * 通过淘宝IP接口获取IP地理位置 * @param string $ip * @return: string **/ function getCityCurl($ip) { $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip; $ch = curl_init(); $timeout = 5; curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $file_contents = curl_exec($ch); curl_close($ch); $ipinfo=json_decode($file_contents); if($ipinfo->code=='1'){ return false; } $city = $ipinfo->data->region.$ipinfo->data->city; return $city; } function getCity($ip) { $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip; $ipinfo=json_decode(file_get_contents($url)); if($ipinfo->code=='1'){ return false; } $city = $ipinfo->data->region.$ipinfo->data->city; return $city; } // for file_get_contents $startTime=explode(' ',microtime()); $startTime=$startTime[0] + $startTime[1]; for($i=1;$i<=10;$i++) { echo getCity("121.207.247.202")."</br>"; } $endTime = explode(' ',microtime()); $endTime = $endTime[0] + $endTime[1]; $totalTime = $endTime - $startTime; echo 'file_get_contents:'.number_format($totalTime, 10, '.', "")." seconds</br>"; //for curl $startTime2=explode(' ',microtime()); $startTime2=$startTime2[0] + $startTime2[1]; for($i=1;$i<=10;$i++) { echo getCityCurl('121.207.247.202')."</br>"; } $endTime2 = explode(' ',microtime()); $endTime2=$endTime2[0] + $endTime2[1]; $totalTime2 = $endTime2 - $startTime2; echo "curl:".number_format($totalTime2, 10, '.', "")." seconds"; ?> |
测试访问
file_get_contents速度:4.2404510975 seconds
curl速度:2.8205530643 seconds
curl比file_get_contents速度快了30%左右,最重要的是服务器负载更低.
一个简单好用的Php自用分页函数,有需要使用分页函数又不想手写的朋友可进入参考。代码如下 | 复制代码 |
/* * 前台分页 * * $page_array = array( * 'totle' =>10, //总数 * 'dispage' =>10, //每页显示条数 * 'url' =>'/article/list/',//url地址 * 'now_page', =>1, //当前所在页数 * ); * * @return pagenav */ function front_page( $page_array ){ //分页判断 $lastpage = ceil($page_array['total'] / $page_array['dispage']); //最后一页 $page_array['now_page'] = min( $lastpage , $page_array['now_page'] );//比较当前页数和最后一页 $prepage = $page_array['now_page'] - 1;//上一页 $nextpage = ($page_array['now_page'] == $lastpage ? 0 : $page_array['now_page'] + 1 );//下一页 $firstcount = ($page_array['now_page']-1) * $page_array['dispage']; if( $lastpage <= 1) return false;//最后一页小于1,则直接返回 //首页 if($prepage){ $page_array[]="<a href='{$page_array['url']}"."1/'>首页</a>"; }else{ $page_array[]="<span class=\"nolink\">首页</span>"; } //上一页 if($prepage) { $page_array[]="<a href='{$page_array['url']}$prepage/'> 上一页 </a> "; }else{ $page_array[]=''; } //显示的数字分页条数 $pagenum=5; $offset=2;//偏移两 $from=$page_array['now_page'] - $offset;//起始 $to=$page_array['now_page'] + $pagenum-$offset-1;//终止 if($pagenum>$lastpage){ $from=1; $to=$lastpage; }else{ if($from<1){ $to=$page_array['now_page'] + 1 - $from; $from=1; if(($to-$from)<$pagenum && ($to-$from)<$lastpage){ $to=$pagenum; } }elseif($to>$lastpage){ $from=$page_array['now_page'] - $lastpage+$to; $to=$lastpage; if(($to-$from)<$pagenum && ($to-$from)<$lastpage){ $from=$lastpage-$pagenum+1; } } } for($i=$from;$i<=$to;$i++){ if($i == $page_array['now_page']){ $pagenav.="<a href=\"#\" title=\"\">$i</a> "; }else{ $pagenav.="<a href=\"{$page_array['url']}$i/\" title=\"\">$i</a> "; } } //数字分页 $page_array[]= $pagenav; //下一页 if($nextpage){ $page_array[]="<a href='{$page_array['url']}"."$nextpage/'> 下一页 </a> "; }else { $page_array[]=''; } //尾页 if($nextpage){ $page_array[]="<a href='{$page_array['url']}"."$lastpage/'>尾页</a>"; }else{ $page_array[]='<span class="nolink">尾页</span>'; } //记录数 $page_array[]="<span>每页 ".$page_array['dispage']." 条</span> <span>共 ".$page_array['total']." 条纪录</span>"; //分页数 $page_array[]="共 ".$lastpage." 页"; //跳页 $page_array[] = '<span>跳转到:</span><input size="4" id="pagenu" type="text" onkeydown="javascript:if(event.keyCode==13){var page=(this.value<0)?1:this.value;location=\''.$page_array['url'].'\'+page;}" /> <a href="javascript:void(0)" onclick="javascript:var spage=(window.document.getElementById(\'pagenu\').value==\'\')?1:window.document.getElementById(\'pagenu\').value;location=\''.$page_array['url'].'\'+spage;">Go</a>'; //第一页总数 $firstcount=$firstcount < 1 ? 0 : $firstcount; return array('pagenav'=>$page_array,'limit'=>$firstcount,'offset'=> $page_array['dispage'],'allpage'=>$lastpage); } |