加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

PHP实用代码片段(三)

发布时间:2020-05-25 03:05:18 所属栏目:PHP 来源:互联网
导读:1. 目录清单使用下面的 PHP 代码片段可以在一个目录中列出所有文件和文件夹。function list_files($dir){if(is_dir($dir)){if($handle = opendir($dir)){while(($file = readdir($handle)) !== false){if($file != . amp;amp; $file != .. amp;amp; $file !=

1. 目录清单

使用下面的 PHP 代码片段可以在一个目录中列出所有文件和文件夹。

list_files(((( = ((( = ()) !== ( != "." && != ".." && != "Thumbs.db" '..'">'..'
'."n"(

语法:

2. 检测用户语言

使用下面的 PHP 代码片段可以检测用户浏览器所使用的语言。

get_client_language(,='en' ((['HTTP_ACCEPT_LANGUAGE'=(',',['HTTP_ACCEPT_LANGUAGE' ( =(,2((,

3. 查看 CSV 文件

readCSV( = (,'r' (!([] = (,1024(

语法:

4. 从 PHP 数据创建 CSV 文件

generateCsv(, = ', = '"' = ('php://temp','r+' ( ,,,( (!( .= (,8192(

语法:

5. 解析 XML 数据

=" ben A h2o K "<span style="color: #008000">//<span style="color: #008000">load the xml string using simplexml function
<span style="color: #800080">$xml = <span style="color: #008080">simplexml_load_string(<span style="color: #800080">$xml_string<span style="color: #000000">);

<span style="color: #008000">//<span style="color: #008000">loop through the each node of molecule
<span style="color: #0000ff">foreach (<span style="color: #800080">$xml->molecule <span style="color: #0000ff">as <span style="color: #800080">$record<span style="color: #000000">)
{
<span style="color: #008000">//<span style="color: #008000">attribute are accessted by
<span style="color: #0000ff">echo <span style="color: #800080">$record['name'],' '<span style="color: #000000">;
<span style="color: #008000">//<span style="color: #008000">node are accessted by -> operator
<span style="color: #0000ff">echo <span style="color: #800080">$record->symbol,' '<span style="color: #000000">;
<span style="color: #0000ff">echo <span style="color: #800080">$record->code,'
'<span style="color: #000000">;
}

6. 解析 JSON 数据

='{"id":1,"name":"rolf","country":"russia","office":["google","oracle"]} '=json_decode( ->name; ->office[0];

7. 获取当前页面 URL

这个 PHP 片段可以帮助你让用户登录后直接跳转到之前浏览的页面。

= "http://" . ['HTTP_HOST'] . ['REQUEST_URI' = ("&","&amp;",

语法:

8. 从任意的 Twitter 账号获取最新的 Tweet

my_twitter( = 1 = "http://search.twitter.com/search.atom?q=from:" . . "&rpp=" . = ((->children() ( (->getName() == "link") = ['href'(->getName() == "content" = . "" '

'

语法:

9. 转发数量

使用这个 PHP 片段可以检测你的页面 URL 有多少转发数量。

tweetCount( = ("http://api.tweetmeme.com/url_info?url=". = SimpleXmlElement( = ->story->( 0

语法:

10. 计算两个日期的差

diff( "difference " . ->y . " years," . ->m." months,".->d." days " "difference " . ->days . " days "--------------------------------------------------------<span style="color: #008000">/<span style="color: #008000">

  • Calculate differences between two dates with precise semantics. Based on PHPs DateTime::diff()
  • implementation by Derick Rethans. Ported to PHP by Emil H,2011-05-02. No rights reserved.
  • See here for original code:
  • http://svn.php.net/viewvc/php/php-src/trunk/ext/date/lib/tm2unixtime.c?revision=302890&amp;view=markup
  • http://svn.php.net/viewvc/php/php-src/trunk/ext/date/lib/interval.c?revision=298973&amp;view=markup
    <span style="color: #008000">/
    <span style="color: #0000ff">function _date_range_limit(<span style="color: #800080">$start,<span style="color: #800080">$end,<span style="color: #800080">$adj,<span style="color: #800080">$a,<span style="color: #800080">$b,<span style="color: #800080">$result<span style="color: #000000">)
    {
    <span style="color: #0000ff">if (<span style="color: #800080">$result[<span style="color: #800080">$a] < <span style="color: #800080">$start<span style="color: #000000">) {
    <span style="color: #800080">$result[<span style="color: #800080">$b] -= <span style="color: #008080">intval((<span style="color: #800080">$start - <span style="color: #800080">$result[<span style="color: #800080">$a] - 1) / <span style="color: #800080">$adj) + 1<span style="color: #000000">;
    <span style="color: #800080">$result[<span style="color: #800080">$a] += <span style="color: #800080">$adj
    <span style="color: #008080">intval((<span style="color: #800080">$start - <span style="color: #800080">$result[<span style="color: #800080">$a] - 1) / <span style="color: #800080">$adj + 1<span style="color: #000000">);
    }
    <span style="color: #0000ff">if (<span style="color: #800080">$result[<span style="color: #800080">$a] >= <span style="color: #800080">$end<span style="color: #000000">) {
    <span style="color: #800080">$result[<span style="color: #800080">$b] += <span style="color: #008080">intval(<span style="color: #800080">$result[<span style="color: #800080">$a] / <span style="color: #800080">$adj<span style="color: #000000">);
    <span style="color: #800080">$result[<span style="color: #800080">$a] -= <span style="color: #800080">$adj <span style="color: #008080">intval(<span style="color: #800080">$result[<span style="color: #800080">$a] / <span style="color: #800080">$adj<span style="color: #000000">);
    }
    <span style="color: #0000ff">return <span style="color: #800080">$result<span style="color: #000000">;
    }
    <span style="color: #0000ff">function _date_range_limit_days(<span style="color: #800080">$base,<span style="color: #800080">$result<span style="color: #000000">)
    {
    <span style="color: #800080">$days_in_month_leap = <span style="color: #0000ff">array(31,31,29,30,31<span style="color: #000000">);
    <span style="color: #800080">$days_in_month = <span style="color: #0000ff">array(31,28,31<span style="color: #000000">);
    _date_range_limit(1,13,12,"m","y",&<span style="color: #800080">$base<span style="color: #000000">);
    <span style="color: #800080">$year = <span style="color: #800080">$base["y"<span style="color: #000000">];
    <span style="color: #800080">$month = <span style="color: #800080">$base["m"<span style="color: #000000">];
    <span style="color: #0000ff">if (!<span style="color: #800080">$result["invert"<span style="color: #000000">]) {
    <span style="color: #0000ff">while (<span style="color: #800080">$result["d"] < 0<span style="color: #000000">) {
    <span style="color: #800080">$month--<span style="color: #000000">;
    <span style="color: #0000ff">if (<span style="color: #800080">$month < 1<span style="color: #000000">) {
    <span style="color: #800080">$month += 12<span style="color: #000000">;
    <span style="color: #800080">$year--<span style="color: #000000">;
    }
    <span style="color: #800080">$leapyear = <span style="color: #800080">$year % 400 == 0 || (<span style="color: #800080">$year % 100 != 0 && <span style="color: #800080">$year % 4 == 0<span style="color: #000000">);
    <span style="color: #800080">$days = <span style="color: #800080">$leapyear ? <span style="color: #800080">$days_in_month_leap[<span style="color: #800080">$month] : <span style="color: #800080">$days_in_month[<span style="color: #800080">$month<span style="color: #000000">];
    <span style="color: #800080">$result["d"] += <span style="color: #800080">$days<span style="color: #000000">;
    <span style="color: #800080">$result["m"]--<span style="color: #000000">;
    }
    } <span style="color: #0000ff">else<span style="color: #000000"> {
    <span style="color: #0000ff">while (<span style="color: #800080">$result["d"] < 0<span style="color: #000000">) {
    <span style="color: #800080">$leapyear = <span style="color: #800080">$year % 400 == 0 || (<span style="color: #800080">$year % 100 != 0 && <span style="color: #800080">$year % 4 == 0<span style="color: #000000">);
    <span style="color: #800080">$days = <span style="color: #800080">$leapyear ? <span style="color: #800080">$days_in_month_leap[<span style="color: #800080">$month] : <span style="color: #800080">$days_in_month[<span style="color: #800080">$month<span style="color: #000000">];
    <span style="color: #800080">$result["d"] += <span style="color: #800080">$days<span style="color: #000000">;
    <span style="color: #800080">$result["m"]--<span style="color: #000000">;
    <span style="color: #800080">$month++<span style="color: #000000">;
    <span style="color: #0000ff">if (<span style="color: #800080">$month > 12<span style="color: #000000">) {
    <span style="color: #800080">$month -= 12<span style="color: #000000">;
    <span style="color: #800080">$year++<span style="color: #000000">;
    }
    }
    }
    <span style="color: #0000ff">return <span style="color: #800080">$result<span style="color: #000000">;
    }
    <span style="color: #0000ff">function _date_normalize(<span style="color: #800080">$base,<span style="color: #800080">$result<span style="color: #000000">)
    {
    <span style="color: #800080">$result = _date_range_limit(0,60,"s","i",<span style="color: #800080">$result<span style="color: #000000">);
    <span style="color: #800080">$result = _date_range_limit(0,"h",24,"d",<span style="color: #800080">$result<span style="color: #000000">);
    <span style="color: #800080">$result = _date_range_limit_days(&<span style="color: #800080">$base,&<span style="color: #800080">$result<span style="color: #000000">);
    <span style="color: #800080">$result = _date_range_limit(0,<span style="color: #800080">$result<span style="color: #000000">);
    <span style="color: #0000ff">return <span style="color: #800080">$result<span style="color: #000000">;
    }
    <span style="color: #008000">/
    <span style="color: #008000">*
  • Accepts two unix timestamps.
    <span style="color: #008000">*/
    <span style="color: #0000ff">function _date_diff(<span style="color: #800080">$one,<span style="color: #800080">$two<span style="color: #000000">)
    {
    <span style="color: #800080">$invert = <span style="color: #0000ff">false<span style="color: #000000">;
    <span style="color: #0000ff">if (<span style="color: #800080">$one > <span style="color: #800080">$two<span style="color: #000000">) {
    <span style="color: #0000ff">list(<span style="color: #800080">$one,<span style="color: #800080">$two) = <span style="color: #0000ff">array(<span style="color: #800080">$two,<span style="color: #800080">$one<span style="color: #000000">);
    <span style="color: #800080">$invert = <span style="color: #0000ff">true<span style="color: #000000">;
    }
    <span style="color: #800080">$key = <span style="color: #0000ff">array("y","s"<span style="color: #000000">);
    <span style="color: #800080">$a = <span style="color: #008080">array_combine(<span style="color: #800080">$key,<span style="color: #008080">array_map("intval",<span style="color: #008080">explode(" ",<span style="color: #008080">date("Y m d H i s",<span style="color: #800080">$one<span style="color: #000000">))));
    <span style="color: #800080">$b = <span style="color: #008080">array_combine(<span style="color: #800080">$key,<span style="color: #800080">$two<span style="color: #000000">))));
    <span style="color: #800080">$result = <span style="color: #0000ff">array<span style="color: #000000">();
    <span style="color: #800080">$result["y"] = <span style="color: #800080">$b["y"] - <span style="color: #800080">$a["y"<span style="color: #000000">];
    <span style="color: #800080">$result["m"] = <span style="color: #800080">$b["m"] - <span style="color: #800080">$a["m"<span style="color: #000000">];
    <span style="color: #800080">$result["d"] = <span style="color: #800080">$b["d"] - <span style="color: #800080">$a["d"<span style="color: #000000">];
    <span style="color: #800080">$result["h"] = <span style="color: #800080">$b["h"] - <span style="color: #800080">$a["h"<span style="color: #000000">];
    <span style="color: #800080">$result["i"] = <span style="color: #800080">$b["i"] - <span style="color: #800080">$a["i"<span style="color: #000000">];
    <span style="color: #800080">$result["s"] = <span style="color: #800080">$b["s"] - <span style="color: #800080">$a["s"<span style="color: #000000">];
    <span style="color: #800080">$result["invert"] = <span style="color: #800080">$invert ? 1 : 0<span style="color: #000000">;
    <span style="color: #800080">$result["days"] = <span style="color: #008080">intval(<span style="color: #008080">abs((<span style="color: #800080">$one - <span style="color: #800080">$two)/86400<span style="color: #000000">));
    <span style="color: #0000ff">if (<span style="color: #800080">$invert<span style="color: #000000">) {
    _date_normalize(&<span style="color: #800080">$a,&<span style="color: #800080">$result<span style="color: #000000">);
    } <span style="color: #0000ff">else<span style="color: #000000"> {
    _date_normalize(&<span style="color: #800080">$b,&<span style="color: #800080">$result<span style="color: #000000">);
    }
    <span style="color: #0000ff">return <span style="color: #800080">$result<span style="color: #000000">;
    }
    <span style="color: #800080">$date = "2014-12-04 19:37:22"<span style="color: #000000">;
    <span style="color: #0000ff">echo '
    '<span style="color: #000000">;
    <span style="color: #008080">print_r( _date_diff( <span style="color: #008080">strtotime(<span style="color: #800080">$date),<span style="color: #008080">time<span style="color: #000000">() ) );
    <span style="color: #0000ff">echo '
    '<span style="color: #000000">;
    ?>

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读