Web开发者必备的10个救命的PHP代码片段_PHP教程
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Web开发者必备的10个救命的PHP代码片段_PHP教程,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3898字,纯文字阅读大概需要6分钟。
内容图文
![Web开发者必备的10个救命的PHP代码片段_PHP教程](/upload/InfoBanner/zyjiaocheng/174/dc2ca9edb6294b1392ecb1f2893ffecf.jpg)
-
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', '$1', $sString);
- }
[代码] 获取你的Feedburner的用户
-
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.google.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);
- }
[代码] 自动生成密码
-
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文件
-
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("rn", "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();
[代码] 获取短网址
-
function getTinyUrl($url) {
- return file_get_contents("http://tinyurl.com/api-create.php?url=".$url);
- }
[代码] 根据生日计算年龄
-
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") – $year;
- $month_diff = date("m") – $month;
- $day_diff = date("d") – $day;
- if ($day_diff < 0 || $month_diff < 0) $year_diff–;
- return $year_diff;
- }
[代码] 计算执行时间
-
//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的维护模式
-
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样式被缓存
-
"/stylesheet.css?" rel="stylesheet" type="text/css" /&glt; </link href=<>
[代码] 为数字增加 stndrd 等
-
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;
- }
http://www.bkjia.com/PHPjc/445722.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445722.htmlTechArticle[代码] 关键词高亮 function highlight( $sString , $aWords ){ if (! is_array ( $aWords )|| empty empty ( $aWords )||! is_string ( $sString )){ return false; } $sWords =implode...
内容总结
以上是互联网集市为您收集整理的Web开发者必备的10个救命的PHP代码片段_PHP教程全部内容,希望文章能够帮你解决Web开发者必备的10个救命的PHP代码片段_PHP教程所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。