您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

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

2024/5/22 14:31:52发布38次查看
[代码] 关键词高亮










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 $month_diff $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;  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;  }  
 href=
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...
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product