搜索
首页php教程php手册Web开发者必备的10个救命的PHP代码片段

Web开发者必备的10个救命的PHP代码片段

Jun 13, 2016 am 11:14 AM
functionphpsweb代码关键词开发者必备片段高亮

 

[代码] 关键词高亮


  1. <span><span class="keyword">function</span><span> highlight(</span><span class="vars">$sString</span><span>, </span><span class="vars">$aWords</span><span>) {  </span></span>
  2.     if (!is_array ($aWords) || emptyempty ($aWords) || !is_string ($sString)) {  
  3.         return false;  
  4.     }  
  5.  
  6.     $sWords = implode ('|'$aWords);  
  7.     return preg_replace ('@b('.$sWords.')b@si''$1'$sString);  

[代码] 获取你的Feedburner的用户


  1. <span><span class="keyword">function</span><span> get_average_readers(</span><span class="vars">$feed_id</span><span>,</span><span class="vars">$interval</span><span> = 7){  </span></span>
  2.     $today = date('Y-m-d'strtotime("now"));  
  3.     $ago = date('Y-m-d'strtotime("-".$interval." days"));  
  4.     $feed_url="https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=".$feed_id."&dates=".$ago.",".$today;  
  5.     $ch = curl_init();  
  6.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
  7.     curl_setopt($ch, CURLOPT_URL, $feed_url);  
  8.     $data = curl_exec($ch);  
  9.     curl_close($ch);  
  10.     $xml = new SimpleXMLElement($data);  
  11.     $fb = $xml->feed->entry['circulation'];  
  12.  
  13.     $nb = 0;  
  14.     foreach($xml->feed->children() as $circ){  
  15.         $nb += $circ['circulation'];  
  16.     }  
  17.  
  18.     return round($nb/$interval);  

[代码] 自动生成密码


  1. <span><span class="keyword">function</span><span> generatePassword(</span><span class="vars">$length</span><span>=9, </span><span class="vars">$strength</span><span>=0) {  </span></span>
  2.     $vowels = 'aeuy';  
  3.     $consonants = 'bdghjmnpqrstvz';  
  4.     if ($strength >= 1) {  
  5.         $consonants .= 'BDGHJLMNPQRSTVWXZ';  
  6.     }  
  7.     if ($strength >= 2) {  
  8.         $vowels .= "AEUY";  
  9.     }  
  10.     if ($strength >= 4) {  
  11.         $consonants .= '23456789';  
  12.     }  
  13.     if ($strength >= 8 ) {  
  14.         $vowels .= '@#$%';  
  15.     }  
  16.  
  17.     $password = '';  
  18.     $alt = time() % 2;  
  19.     for ($i = 0; $i $length$i++) {  
  20.         if ($alt == 1) {  
  21.             $password .= $consonants[(rand() % strlen($consonants))];  
  22.             $alt = 0;  
  23.         } else {  
  24.             $password .= $vowels[(rand() % strlen($vowels))];  
  25.             $alt = 1;  
  26.         }  
  27.     }  
  28.     return $password;  

[代码] 压缩多个CSS文件


  1. <span><span>header(</span><span class="string">'Content-type: text/css'</span><span>);  </span></span>
  2. ob_start("compress");  
  3. function compress($buffer) {  
  4.   /* remove comments */ 
  5.   $buffer = preg_replace('!/*[^*]**+([^/][^*]**+)*/!'''$buffer);  
  6.   /* remove tabs, spaces, newlines, etc. */ 
  7.   $buffer = str_replace(array("rn""r""n""t"'  ''    ''    '), ''$buffer);  
  8.   return $buffer;  
  9. }  
  10.  
  11. /* your css files */ 
  12. include('master.css');  
  13. include('typography.css');  
  14. include('grid.css');  
  15. include('print.css');  
  16. include('handheld.css');  
  17.  
  18. ob_end_flush(); 

[代码] 获取短网址


  1. <span><span class="keyword">function</span><span> getTinyUrl(</span><span class="vars">$url</span><span>) {  </span></span>
  2.     return file_get_contents("http://tinyurl.com/api-create.php?url=".$url);  

[代码] 根据生日计算年龄


  1. <span><span class="keyword">function</span><span> age(</span><span class="vars">$date</span><span>){  </span></span>
  2.     $year_diff = '';  
  3.     $time = strtotime($date);  
  4.     if(FALSE === $time){  
  5.         return '';  
  6.     }  
  7.  
  8.     $date = date('Y-m-d'$time);  
  9.     list($year,$month,$day) = explode("-",$date);  
  10.     $year_diff = date("Y") – $year;  
  11.     $month_diff = date("m") – $month;  
  12.     $day_diff = date("d") – $day;  
  13.     if ($day_diff $month_diff $year_diff–;  
  14.  
  15.     return $year_diff;  

[代码] 计算执行时间


  1. <span><span class="comment">//Create a variable for start time </span><span> </span></span>
  2. $time_start = microtime(true);  
  3.  
  4. // Place your PHP/HTML/JavaScript/CSS/Etc. Here  
  5.  
  6. //Create a variable for end time  
  7. $time_end = microtime(true);  
  8. //Subtract the two times to get seconds  
  9. $time = $time_end - $time_start;  
  10.  
  11. echo 'Script took '.$time.' seconds to execute'

[代码] PHP的维护模式


  1. <span><span class="keyword">function</span><span> maintenance(</span><span class="vars">$mode</span><span> = FALSE){  </span></span>
  2.     if($mode){  
  3.         if(basename($_SERVER['SCRIPT_FILENAME']) != 'maintenance.php'){  
  4.             header("Location: http://example.com/maintenance.php");  
  5.             exit;  
  6.         }  
  7.     }else{  
  8.         if(basename($_SERVER['SCRIPT_FILENAME']) == 'maintenance.php'){  
  9.             header("Location: http://example.com/");  
  10.             exit;  
  11.         }  
  12.     }  

[代码] 阻止CSS样式被缓存


  1. <span><span><link span="">
    <span class="string">"/stylesheet.css?<!--?php echo time(); ?-->"</span><span> rel=</span><span class="string">"stylesheet"</span><span> type=</span><span class="string">"text/css"</span><span> /&glt; </span> href=</span></span>

[代码] 为数字增加 stndrd 等


  1. <span><span class="keyword">function</span><span> make_ranked(</span><span class="vars">$rank</span><span>) {  </span></span>
  2.     $last = substr$rank, -1 );  
  3.     $seclast = substr$rank, -2, -1 );  
  4.     if$last > 3 || $last == 0 ) $ext = 'th';  
  5.     else if$last == 3 ) $ext = 'rd';  
  6.     else if$last == 2 ) $ext = 'nd';  
  7.     else $ext = 'st';   
  8.  
  9.     if$last == 1 && $seclast == 1) $ext = 'th';  
  10.     if$last == 2 && $seclast == 1) $ext = 'th';  
  11.     if$last == 3 && $seclast == 1) $ext = 'th';   
  12.  
  13.     return $rank.$ext;  

 

 href=
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它们
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。