仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1689|回复: 18
打印 上一主题 下一主题

[学习教程] PHP网页编程之10 个救命的 PHP 代码片断

[复制链接]
变相怪杰 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-3 23:35:26 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
你的留言本应该加入注册以及分页功能了,而如果你更强的话,UI(用户界面)也可以加强,完成之后,感觉是不是特有成就感?不管怎么样,咱好歹是写了一个动态网站程序了,放在自己的网站上耍耍吧。      关头词高亮
  1. function highlight($sString, $aWords) {     if (!is_array ($aWords)  emptyempty ($aWords)  !is_string ($sString)) {         return false;     }      $sWords = implode ('', $aWords);     return preg_replace ('@\b('.$sWords.')\b@si', '<strong style="background-color:yellow">$1</strong>', $sString); }
复制代码
  获得你的Feedburner的用户
  1. function get_average_readers($feed_id,$interval = 7){     $today = date('Y-m-d', strtotime("now"));     $ago = date('Y-m-d', strtotime("-".$interval." days"));     $feed_url="https://feedburner.谷歌
    .com/api/awareness/1.0/GetFeedData?uri=".$feed_id."&dates=".$ago.",".$today;     $ch = curl_init();     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);     curl_setopt($ch, CURLOPT_URL, $feed_url);     $data = curl_exec($ch);     curl_close($ch);     $xml = new SimpleXMLElement($data);     $fb = $xml->feed->entry['circulation'];      $nb = 0;     foreach($xml->feed->children() as $circ){         $nb += $circ['circulation'];     }      return round($nb/$interval); }
复制代码

    主动生成暗码
  1. function generatePassword($length=9, $strength=0) {     $vowels = 'aeuy';     $consonants = 'bdghjmnpqrstvz';     if ($strength >= 1) {         $consonants .= 'BDGHJLMNPQRSTVWXZ';     }     if ($strength >= 2) {         $vowels .= "AEUY";     }     if ($strength >= 4) {         $consonants .= '23456789';     }     if ($strength >= 8 ) {         $vowels .= '@#$%';     }      $password = '';     $alt = time() % 2;     for ($i = 0; $i < $length; $i++) {         if ($alt == 1) {             $password .= $consonants[(rand() % strlen($consonants))];             $alt = 0;         } else {             $password .= $vowels[(rand() % strlen($vowels))];             $alt = 1;         }     }     return $password; }
复制代码

    紧缩多个CSS文件
  1. header('Content-type: text/css'); ob_start("compress"); function compress($buffer) {   /* remove comments */   $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);   /* remove tabs, spaces, newlines, etc. */   $buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer);   return $buffer; }  /* your css files */ include('master.css'); include('typography.css'); include('grid.css'); include('print.css'); include('handheld.css');  ob_end_flush();
复制代码

    获得短网址
  1. function getTinyUrl($url) {     return file_get_contents("http://tinyurl.com/api-create.php?url=".$url); }
复制代码

    依据诞辰盘算岁数
  1. function age($date){     $year_diff = '';     $time = strtotime($date);     if(FALSE === $time){         return '';     }      $date = date('Y-m-d', $time);     list($year,$month,$day) = explode("-",$date);     $year_diff = date("Y") &ndash; $year;     $month_diff = date("m") &ndash; $month;     $day_diff = date("d") &ndash; $day;     if ($day_diff < 0  $month_diff < 0) $year_diff&ndash;;      return $year_diff; }
复制代码

    盘算履行工夫
  1. //Create a variable for start time $time_start = microtime(true);  // Place your PHP/HTML/JavaScript/CSS/Etc. Here  //Create a variable for end time $time_end = microtime(true); //Subtract the two times to get seconds $time = $time_end - $time_start;  echo 'Script took '.$time.' seconds to execute';
复制代码

    PHP的保护形式
  1. function maintenance($mode = FALSE){     if($mode){         if(basename($_SERVER['SCRIPT_FILENAME']) != 'maintenance.php'){             header("Location: http://example.com/maintenance.php");             exit;         }     }else{         if(basename($_SERVER['SCRIPT_FILENAME']) == 'maintenance.php'){             header("Location: http://example.com/");             exit;         }     } }
复制代码

    禁止CSS款式被缓存
  1. <link href="/stylesheet.css?<?php echo time(); ?>" rel="stylesheet" type="text/css" /&glt;
复制代码

    为数字增添 st\nd\rd 等
  1. function make_ranked($rank) {     $last = substr( $rank, -1 );     $seclast = substr( $rank, -2, -1 );     if( $last > 3  $last == 0 ) $ext = 'th';     else if( $last == 3 ) $ext = 'rd';     else if( $last == 2 ) $ext = 'nd';     else $ext = 'st';       if( $last == 1 && $seclast == 1) $ext = 'th';     if( $last == 2 && $seclast == 1) $ext = 'th';     if( $last == 3 && $seclast == 1) $ext = 'th';       return $rank.$ext; }
复制代码

<P style="TEXT-INDENT: 2em">
让好朋友来看看,嘿,看咱写的多棒,然后再在网上宣传一下。
变相怪杰 该用户已被删除
沙发
 楼主| 发表于 2015-4-26 20:11:55 | 显示全部楼层
我要在声明一下:我是个菜鸟!!我对php这门优秀的语言也是知之甚少。但是我要在这里说一下php在网站开发中最常用的几个功能:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-18 11:43

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表