您好,欢迎访问一九零五行业门户网

7 个超级实用的 PHP 代码片段_PHP教程

关键的时候能拿得出关键代码才是好的程序员。这篇文章里芒果小站收集了一些诸如此类的关键代码,有用于编程。
1、超级简单的页面缓存如果你的工程项目不是基于 cms 系统或框架,打造一个简单的缓存系统将会非常实在。下面的代码很简单,但是对小网站而言能切切实实解决问题。
0, days => 0, hours => 0,minutes => 0, seconds => 0,);if($time >= 31556926){$value[years] = floor($time/31556926);$time = ($time%31556926);}if($time >= 86400){$value[days] = floor($time/86400);$time = ($time%86400);}if($time >= 3600){$value[hours] = floor($time/3600);$time = ($time%3600);}if($time >= 60){$value[minutes] = floor($time/60);$time = ($time%60);}$value[seconds] = floor($time);return (array) $value;}else{return (bool) false;}}
点击这里查看详细情况:http://ckorp.net/sec2time.php
4、强制下载文件一些诸如 mp3 类型的文件,通常会在客户端浏览器中直接被播放或使用。如果你希望它们强制被下载,也没问题。可以使用以下代码:
function downloadfile($file){$file_name = $file;$mime = 'application/force-download';  header('pragma: public');  // required  header('expires: 0');  // no cache  header('cache-control: must-revalidate, post-check=0, pre-check=0');  header('cache-control: private',false);  header('content-type: '.$mime);  header('content-disposition: attachment; filename='.basename($file_name).'');  header('content-transfer-encoding: binary');  header('connection: close');  readfile($file_name);  // push it out  exit();}
点击这里查看详细情况:credit: alessio delmonti
5、使用 google api 获取当前天气信息想知道今天的天气?这段代码会告诉你,只需 3 行代码。你只需要把其中的 address 换成你期望的城市。
$xml = simplexml_load_file('http://www.google.com/ig/api?weather=address');$information = $xml->xpath(/xml_api_reply/weather/current_conditions/condition);echo $information[0]->attributes();
点击这里查看详细情况:http://ortanotes.tumblr.com/post/200469319/current-weather-in-3-lines-of-php
6、获得某个地址的经纬度随着 google maps api 的普及,开发人员常常需要获得某一特定地点的经度和纬度。这个非常有用的函数以某一地址作为参数,返回一个数组,包含经度和纬度数据。
___fckpd___6
点击这里查看详细情况:http://snipplr.com/view.php?codeview&id=47806
7、使用 php 和 google 获取域名的 favicon 图标有些网站或 web 应用程序需要使用来自其他网站的 favicon 图标。利用 google 和 php 很容易就能搞定,不过前提是 google 不会连接被重置哦!
function get_favicon($url){$url = str_replace(http://,'',$url);return http://www.google.com/s2/favicons?domain=.$url;}
点击这里查看详细情况:http://snipplr.com/view.php?codeview&id=45928
参考资料:10 super useful php snippets
http://www.bkjia.com/phpjc/363929.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/363929.htmltecharticle关键的时候能拿得出关键代码才是好的程序员。这篇文章里芒果小站收集了一些诸如此类的关键代码,有用于编程。 1、超级简单的页面缓...
其它类似信息

推荐信息