国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php開發中實用的PHP代碼片段

2024-05-04 21:47:53
字體:
來源:轉載
供稿:網友

1.關鍵詞高亮,代碼如下:

  1. function highlight($sString$aWords) { 
  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); 

2.獲取你的Feedburner的用戶,代碼如下:

  1. function get_average_readers($feed_id,$interval = 7){ 
  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); 

3.自動生成密碼,代碼如下:

  1. function generatePassword($length=9, $strength=0) { 
  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

4.壓縮多個CSS文件,代碼如下:

  1. header('Content-type: text/css'); 
  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. /* your css files */ 
  11. include('master.css'); 
  12. include('typography.css'); 
  13. include('grid.css'); 
  14. include('print.css'); 
  15. include('handheld.css'); 
  16.  
  17. ob_end_flush(); 

5.獲取短網址,代碼如下:

  1. function getTinyUrl($url) { 
  2.     return file_get_contents("http://tinyurl.com/api-create.php?url=".$url); 
  3. }  

6.根據生日計算年齡,代碼如下:

  1. function age($date){ 
  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 < 0 || $month_diff < 0) $year_diff–; 
  14.  
  15.  return $year_diff

7.計算執行時間,代碼如下:

  1. //Create a variable for start time 
  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';8.PHP的維護模式 
  12. function maintenance($mode = FALSE){ 
  13.     if($mode){ 
  14.  
  15.         if(basename($_SERVER['SCRIPT_FILENAME']) != 'maintenance.php'){ 
  16.             header("Location: http://example.com/maintenance.php"); 
  17.             exit
  18.         } 
  19.     }else
  20.         if(basename($_SERVER['SCRIPT_FILENAME']) == 'maintenance.php'){ 
  21.             header("Location: http://example.com/"); 
  22.             exit
  23.         } 
  24.     } 

通過IP判斷來源,這是一個非常實用的代碼片段,可以幫助你通過IP來判斷訪客來源,下面的方法通過接收一個參數,然后返回IP所在地點,如果沒有找到,則返回UNKNOWN,代碼如下:

  1. function detect_city($ip) {  
  2.    
  3.         $default = 'UNKNOWN';  
  4.    
  5.         if (!is_string($ip) || strlen($ip) < 1 || $ip == '127.0.0.1' || $ip == 'localhost')  
  6.             $ip = '8.8.8.8';  
  7.    
  8.         $curlopt_useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)';  
  9.    
  10.         $url = 'http://ipinfodb.com/ip_locator.php?ip=' . urlencode($ip);  
  11.         $ch = curl_init();  
  12.    
  13.         $curl_opt = array(  
  14.             CURLOPT_FOLLOWLOCATION  => 1,  
  15.             CURLOPT_HEADER      => 0,  
  16.             CURLOPT_RETURNTRANSFER  => 1,  
  17.             CURLOPT_USERAGENT   => $curlopt_useragent,  
  18.             CURLOPT_URL       => $url,  
  19.             CURLOPT_TIMEOUT         => 1,  
  20.             CURLOPT_REFERER         => 'http://' . $_SERVER['HTTP_HOST'],  
  21.         );  
  22.    
  23.         curl_setopt_array($ch$curl_opt);  
  24.    
  25.         $content = curl_exec($ch);  
  26.    
  27.         if (!is_null($curl_info)) {  
  28.             $curl_info = curl_getinfo($ch);  
  29.         }  
  30.    
  31.         curl_close($ch);  
  32.    
  33.         if ( preg_match('{
  34. City : ([^<]*)
  35. }i'$content$regs) )  {  
  36.             $city = $regs[1];  
  37.         }  
  38.         if ( preg_match('{
  39. State/Province : ([^<]*)
  40. }i'$content$regs) )  {  
  41.             $state = $regs[1];  
  42.         }  
  43.    
  44.         if$city!='' && $state!='' ){  
  45.           $location = $city . ', ' . $state;  
  46.           return $location;  
  47.         }else{  
  48.           return $default;  
  49.         }  
  50.    
  51.     }  

判斷一張圖片的主色調,下面這個代碼非常實用,能幫助你判斷一張圖片中的主色調,你可以分析任何圖片,代碼如下:

  1. $i = imagecreatefromjpeg("image.jpg");  
  2.    
  3. for ($x=0;$x$i);$x++) {  
  4.     for ($y=0;$y$i);$y++) {  
  5.         $rgb = imagecolorat($i,$x,$y);  
  6.         $r   = ($rgb >> 16) & 0xFF;  
  7.         $g   = ($rgb >>  & 0xFF;  
  8.         $b   = $rgb & 0xFF;  
  9.    
  10.         $rTotal += $r;  
  11.         $gTotal += $g;  
  12.         $bTotal += $b;  
  13.         $total++;  
  14.     }  
  15. }  
  16.    
  17. $rAverage = round($rTotal/$total);  
  18. $gAverage = round($gTotal/$total);  
  19. $bAverage = round($bTotal/$total); 

不顯示PHP錯誤而發送電子郵件取代之,如果你不想在頁面中顯示PHP錯誤,也可以通過email來獲取錯誤信息,下面的代碼可以幫助你實現.

  1. // Our custom error handler  
  2. function nettuts_error_handler($number$message$file$line$vars){  
  3.     $email = "  
  4.         

    An error ($number) occurred on line  

  5.         $line and in the file: $file.  
  6.         

     $message 

    ";  
  7.    
  8.     $email .= "
    " . print_r($vars, 1) . "
    ";  
  9.    
  10.     $headers = 'Content-type: text/html; charset=iso-8859-1' . "rn";  
  11.    
  12.     // Email the error to someone...  
  13.     error_log($email, 1, 'you@youremail.com'$headers);  
  14.    
  15.     // Make sure that you decide how to respond to errors (on the user's side)  
  16.     // Either echo an error message, or kill the entire project. Up to you...  
  17.     // The code below ensures that we only "die" if the error was more than  
  18.     // just a NOTICE.  
  19.     if ( ($number !== E_NOTICE) && ($number < 2048) ) {  
  20.         die("There was an error. Please try again later.");  
  21.     }  
  22. }  
  23.    
  24. // We should use our custom function to handle errors.  
  25. set_error_handler('nettuts_error_handler');  
  26.    
  27. // Trigger an error... (var doesn't exist)  
  28. echo $somevarthatdoesnotexist;  

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 通海县| 广德县| 吴忠市| 色达县| 邯郸市| 荔波县| 白城市| 遂平县| 邛崃市| 甘孜县| 贺兰县| 台湾省| 固原市| 郯城县| 崇文区| 白玉县| 宁河县| 铅山县| 黎平县| 东阿县| 大方县| 奇台县| 东丰县| 龙口市| 漳州市| 新密市| 乳山市| 灌云县| 麦盖提县| 扶风县| 临泽县| 蓬溪县| 肥西县| 永宁县| 富阳市| 汝南县| 锡林郭勒盟| 安平县| 延川县| 淮北市| 山西省|