激情小说强奸乱伦亚洲色图_欧美xxxx性受黑人xyx性爽_国产www亚洲а∨天堂一区_日本无码线亚洲中文精品_国产女厕所盗摄老师厕所嘘嘘_香蕉视频在线观看亚洲_久久国产亚洲AV无码麻软件_18禁白浆欧美一区二区三区_国产午夜av无码无片久久96_国产成人精品福利视频

27
18911184380
當前位置:首頁 > 資訊 > 建站知識

[北京網(wǎng)站制作]PHP二十一段救命代碼

2011-10-21 酷站科技

1. PHP可閱讀文章隨機字符串

  此編碼將建立一個可閱讀文章的字符串數(shù)組,使其更貼近字典中的英語單詞,好用且具備登陸密碼認證作用。

  1. /************** 
  2. *@length - length of random string (must be a multiple of 2) 
  3. **************/ 
  4. function readable_random_string($length = 6){ 
  5.     $conso=array("b","c","d","f","g","h","j","k","l"
  6.     "m","n","p","r","s","t","v","w","x","y","z"); 
  7.     $vocal=array("a","e","i","o","u"); 
  8.     $password=""
  9.     srand ((double)microtime()*1000000); 
  10.     $max = $length/2; 
  11.     for($i=1; $i<=$max$i
  12.     { 
  13.     $password.=$conso[rand(0,19)]; 
  14.     $password.=$vocal[rand(0,4)]; 
  15.     } 
  16.     return $password

2. PHP轉(zhuǎn)化成一個隨機字符串

  假如不用可閱讀文章的字符串數(shù)組,應用此涵數(shù)取代,就可以建立一個隨機字符串,做為客戶的隨機密碼等。

  1. /************* 
  2. *@l - length of random string 
  3. */ 
  4. function generate_rand($l){ 
  5.   $c"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
  6.   srand((double)microtime()*1000000); 
  7.   for($i=0; $i<$l$i ) { 
  8.       $rand.= $c[rand()%strlen($c)]; 
  9.   } 
  10.   return $rand

3. PHP編號電子郵箱詳細地址

  應用此編碼,能夠?qū)⒁磺须娮余]箱詳細地址編號為 html 標識符實體線,以避免 被垃圾短信程序流程搜集。

  1. function encode_email($email='[email protected]'$linkText='Contact Us'$attrs ='class="emailencoder"' ) 
  2.     // remplazar aroba y puntos 
  3.     $email = str_replace('@''&#64;'$email); 
  4.     $email = str_replace('.''&#46;'$email); 
  5.     $email = str_split($email, 5);   
  6.  
  7.     $linkText = str_replace('@''&#64;'$linkText); 
  8.     $linkText = str_replace('.''&#46;'$linkText); 
  9.     $linkText = str_split( $linkText, 5);   
  10.  
  11.     $part1 = '<a href="ma'
  12.     $part2 = 'ilto&#58;'
  13.     $part3 = '" '$attrs .' >'
  14.     $part4 = '</a>';   
  15.  
  16.     $encoded = '<script type="text/javascript">'
  17.     $encoded .= "document.write('$part1');"
  18.     $encoded .= "document.write('$part2');"
  19.     foreach($email as $e
  20.     { 
  21.             $encoded .= "document.write('$e');"
  22.     } 
  23.     $encoded .= "document.write('$part3');"
  24.     foreach($linkText as $l
  25.     { 
  26.             $encoded .= "document.write('$l');"
  27.     } 
  28.     $encoded .= "document.write('$part4');"
  29.     $encoded .= '</script>';   
  30.  
  31.     return $encoded

4. PHP認證郵箱地址

  電子郵箱認證或許是中最常見的網(wǎng)頁頁面表單驗證,此編碼除開認證電子郵箱詳細地址,還可以挑選查驗電子郵件域隸屬 DNS 中的 MX 紀錄,使電子郵件認證作用更為強勁。

  1. function  is_valid_email($email$test_mx = false) 
  2.     if(eregi("^([_a-z0-9-] )(.[_a-z0-9-] )*@([a-z0-9-] )(.[a-z0-9-] )*(.[a-z]{2,4})$"$email)) 
  3.         if($test_mx
  4.         { 
  5.             list($username$domain) = split("@"$email); 
  6.             return getmxrr($domain$mxrecords); 
  7.         } 
  8.         else 
  9.             return true; 
  10.     else 
  11.         return false; 

5. PHP列舉文件目錄內(nèi)容

  1. function list_files($dir
  2.     if(is_dir($dir)) 
  3.     { 
  4.         if($handle = opendir($dir)) 
  5.         { 
  6.             while(($file = readdir($handle)) !== false) 
  7.             { 
  8.                 if($file != "." && $file != ".." && $file != "Thumbs.db"
  9.             ;     { 
  10.                     echo '<a target="_blank" href="'.$dir.$file.'">'.$file.'</a><br>'."n"
  11.                 } 
  12.             } 
  13.             closedir($handle); 
  14.         } 
  15.     } 

6. PHP消毀文件目錄

  刪掉一個文件目錄,包含它的內(nèi)容。

  1. /***** 
  2. *@dir - Directory to destroy 
  3. *@virtual[optional]- whether a virtual directory 
  4. */ 
  5. function destroyDir($dir$virtual = false) 
  6.     $ds = DIRECTORY_SEPARATOR; 
  7.     $dir = $virtual ? realpath($dir) : $dir
  8.     $dir = substr($dir, -1) == $ds ? substr($dir, 0, -1) : $dir
  9.     if (is_dir($dir) && $handle = opendir($dir)) 
  10.     { 
  11.         while ($file = readdir($handle)) 
  12.         { 
  13.             if ($file == '.' || $file& nbsp;== '..'
  14.             { 
  15.                 continue
  16.             } 
  17.             elseif (is_dir($dir.$ds.$file)) 
  18.             { 
  19.                 destroyDir($dir.$ds.$file); 
  20.             } 
  21.             else 
  22.             { 
  23.                 unlink($dir.$ds.$file); 
  24.             } 
  25.         } 
  26.         closedir($handle); 
  27.         rmdir($dir); 
  28.         return true; 
  29.     } 
  30.     else 
  31.     { 
  32.         return false; 
  33.     } 

7. PHP分析 JSON 數(shù)據(jù)信息

  與大部分時興的 Web 服務項目如 twitter 根據(jù)對外開放 API 來給出的數(shù)據(jù)一樣,它一直可以了解怎樣分析 API 數(shù)據(jù)信息的各種各樣傳輸文件格式,包含 JSON,XML 這些。

  1. $json_string='{"id":1,"name":"foo","email":"[email protected]","interest":["wordpress","php"]} '
  2. $obj=json_decode($json_string); 
  3. echo $obj->name; //prints foo 
  4. echo $obj->interest[1]; //p rints php 

8. PHP分析 XML 數(shù)據(jù)信息

  1. //xml string 
  2. $xml_string="<?xml version='1.0'?> 
  3. <users> 
  4. <user id='398'
  5. <name>Foo</name> 
  6. <email>[email protected]</name> 
  7. </user> 
  8. <user id='867'
  9. <name>Foobar</name> 
  10. <email>[email protected]</name> 
  11. </user> 
  12. </users>";  
  13.  
  14. //load the xml string using simplexml 
  15. $xml = simplexml_load_string($xml_string);  
  16.  
  17. //loop through the each node of user 
  18. foreach ($xml->user as $user
  19. //access attribute 
  20. echo $user['id'], ' '
  21. //subnodes are accessed by -> operator 
  22. echo $user->name, ' '
  23. echo $user->email, '<br />'

9. PHP建立系統(tǒng)日志縮略名

  建立客戶友善的系統(tǒng)日志縮略名。

  1. function create_slug($string){ 
  2. $slug=preg_replace('/[^A-Za-z0-9-] /''-'$string); 
  3. return $slug

10. PHP獲得手機客戶端真正 IP 詳細地址

  該涵數(shù)將獲得客戶的真正 IP 詳細地址,就算他應用服務器代理。

  1. function getRealIpAddr() 
  2.     if (!emptyempty($_SERVER['HTTP_CLIE NT_IP'])) 
  3.     { 
  4.         $ip=$_SERVER['HTTP_CLIENT_IP']; 
  5.     } 
  6.     elseif (!emptyempty($_SERVER['HTTP_X_FORWARDED_FOR'])) 
  7.     //to check ip is pass from proxy 
  8.     { 
  9.         $ip=$_SERVER['HTTP_X_FORWARDED_FOR']; 
  10.     } 
  11.     else 
  12.     { 
  13.         $ip=$_SERVER['REMOTE_ADDR']; 
  14.     } 
  15.     return $ip

11. PHP強制壓縮文件下載

  為客戶出示強制的壓縮文件下載作用。

  1. /******************** 
  2. *@file - path to file 
  3. */ 
  4. function force_download($file
  5. if ((isset($file))&&(file_exists($file))) { 
  6. header("Content-length: ".filesize($file)); 
  7. header('Content-Type: application/octet-stream'); 
  8. header('Content-Disposition: attachment; filename="' . $file . '"'); 
  9. readfile("$file"); 
  10. else { 
  11. echo "No file selected"

12. PHP建立標簽云

  1. function getCloud( $data = array(),& nbsp;$minFontSize = 12, $maxFontSize = 30 ) 
  2. $minimumCount = min( array_values$data ) ); 
  3. $maximumCount = max( array_values$data ) ); 
  4. $spread = $maximumCount - $minimumCount
  5. $cloudHTML = ''
  6. $cloudTags = array();  
  7.  
  8. $spread == 0 && $spread = 1;  
  9.  
  10. foreach$data as $tag => $count ) 
  11. $size = $minFontSize   ( $count - $minimumCount ) 
  12. * ( $maxFontSize - $minFontSize ) / $spread
  13. $cloudTags[] = '<a style="font-size: ' . floor$size ) . 'px' 
  14. '" href="#" title="'' . $tag . 
  15. '' returned a count of ' . $count . '">' 
  16. . htmlspecialchars( stripslashes$tag ) ) . '</a>'
  17. }  
  18.  
  19. return join( "n"$cloudTags ) . "n"
  20. /************************** 
  21. **** Sample usage ***/ 
  22. $arr = Array('Actionscript' => 35, 'Adobe' => 22, 'Array' => 44, 'Background' => 43, 
  23. 'Blur' => 18, 'Canvas' => 33, 'Class' => 15, 'Color Palette' => 11, 'Crop' => 42, 
  24. 'Delimiter' => 13, 'Depth' => 34, 'Design' => 8, 'Encode' => 12, 'Encryption' => 30, 
  25. 'Extract' => 28, 'Filters' => 42); 
  26. echo getCloud($arr, 12, 36); 

13. PHP找尋2個字符串數(shù)組的相似度

  PHP 出示了一個非常少應用的 similar_text 涵數(shù),但此涵數(shù)十分有效,用以較為2個字符串數(shù)組并回到類似水平的百分數(shù)。

  1. similar_text($string1$string2$percent); 
  2. //$percent will have the percentage of similarity 

14. PHP在程序運行中應用 Gravatar 通用性頭像圖片

  伴隨著 WordPress 愈來愈普及化,Gravatar 也隨著時興。因為 Gravatar 出示了便于應用的 API,將其列入程序運行也越來越十分便捷。

  1. /****************** 
  2. *@email - Email address to show gravatar for 
  3. *@size - size of gravatar 
  4. *@default - URL of default gravatar to use 
  5. *@rating - rating of Gravatar(G, PG, R, X) 
  6. */ 
  7. function show_gravatar($email$size$default$rating
  8. echo '<img src="http://www.gravatar.com/avatar.php?gravatar_id='.md5($email). 
  9. '&default='.$default.'&size='.$size.'&rating='.$rating.'" width="'.$size.'px" 
  10. height="'.$size.'px" />'; 

15. PHP在標識符中斷點處斷開文本

  說白了斷字 (word break),即一個英語單詞可在改行時斷掉的地區(qū)。這一涵數(shù)將在斷字處斷開字符串數(shù)組。

  1. // Original PHP code by Chirp Internet: www.chirp.com.au 
  2. // Please acknowledge use of this code by including this header. 
  3. function myTruncate($string$limit$break="."$pad="...") { 
  4. // return with no change if string is shorter than $limit 
  5. if(strlen($string) <= $limit
  6. return $string;  
  7.  
  8. // is $break present between $limit and the end of the string? 
  9. if(false !== ($breakpoint = strpos($string$break$limit))) { 
  10. if($breakpoint < strlen($string) - 1) { 
  11. $string = substr($string, 0, $breakpoint) . $pad
  12. return $string
  13. /***** Example ****/ 
  14. $short_string=myTruncate($long_string, 100, ' '); 

16. PHP文檔 Zip 縮小

  1. /* creates a compressed zip file */ 
  2. function create_zip($files = array(), $destination = '',$overwrite = false) { 
  3. //if the zip file already exists and overwrite is false, return false 
  4. if(file_exists($destination) && !$overwrite) { return false; } 
  5. //vars 
  6. $valid_files = array(); 
  7. //if files were passed in... 
  8. if(is_array($files)) { 
  9. //cycle through each file 
  10. foreach($files as $file) { 
  11. //make sure the file exists 
  12. if(file_exists($file)) { 
  13. $valid_files[] = $file
  14. //if we have good files... 
  15. if(count($valid_files)) { 
  16. //create the archive 
  17. $zip = new ZipArchive(); 
  18. if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { 
  19. return false; 
  20. //add the files 
  21. foreach($valid_files as $file) { 
  22. $zip->addFile($file,$file); 
  23. //debug 
  24. //echo 'The zip archive& nbsp;contains ',$zip->numFiles,' files with a status of ',$zip->status;  
  25.  
  26. //close the zip -- done! 
  27. $zip->close();  
  28.  
  29. //check to make sure the file exists 
  30. return file_exists($destination); 
  31. else 
  32. return false; 
  33. /***** Example Usage ***/ 
  34. $files=array('file1.jpg''file2.jpg''file3.gif'); 
  35. create_zip($files'myzipfile.zip', true); 

17. PHP壓縮包解壓 Zip 文檔

  1. /********************** 
  2. *@file - path to zip file 
  3. *@destination - destination directory for unzipped files 
  4. */ 
  5. function unzip_file($file$destination){ 
  6. // create object 
  7. $zip = new ZipArchive() ; 
  8. // open archive 
  9. if ($zip->open($file) !== TRUE) { 
  10. die (’Could not open archive’); 
  11. // extract contents to destination directory 
  12. $zip->extractTo($destination); 
  13. // close archive 
  14. $zip->close(); 
  15. echo 'Archive extracted to directory'

18. PHP為 URL 詳細地址預置 http 字符串數(shù)組

  有時候必須接納一些表格中的網(wǎng)站地址 鍵入,但客戶非常少加上 http:// 字段名,此編碼將為網(wǎng)站地址加上該字段名。

  1. if (!preg_match("/^(http|ftp):/"$_POST['url'])) { 
  2.    $_POST['url'] = 'http://'.$_POST['url']; 

19. PHP將網(wǎng)站地址字符串數(shù)組轉(zhuǎn)化成超鏈接

  該涵數(shù)將 URL 和 E-mail 詳細地址字符串數(shù)組變換為可點一下的超鏈接。

  1. function makeClickableLinks($text) { 
  2. $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_ .~#?&//=] )'
  3. '<a href="1">1</a>'$text); 
  4. $text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_ .~#?&//=] )'
  5. '1<a href="http://2">2</a>'$text); 
  6. $text = eregi_replace('([_.0-9a-z-] @([0-9a-z][0-9a-z-] .) [a-z]{2,3})'
  7. '<a href="mailto:1">1</a>'$text);  
  8.  
  9. return $text

20. PHP調(diào)節(jié)圖像尺寸

  建立圖象縮列圖必須很多時間,此編碼將有利于掌握縮列圖的邏輯性。

  1. /********************** 
  2. *@filename - path to the image 
  3. *@tmpname - temporary path to thumbnail 
  4. *@xmax - max width 
  5. *@ymax - max height 
  6. */ 
  7. function resize_image($filename$tmpname$xmax$ymax
  8.     $ext = explode("."$filename); 
  9.     $ext = $ext[< span class="func">count($ext)-1];   
  10.  
  11.     if($ext == "jpg" || $ext == "jpeg"
  12.         $im = imagecreatefromjpeg($tmpname); 
  13.     elseif($ext == "png"
  14.         $im = imagecreatefrompng($tmpname); 
  15.     elseif($ext == "gif"
  16.         $im = imagecreatefromgif($tmpname);   
  17.  
  18.     $x = imagesx($im); 
  19.     $y = imagesy($im);   
  20.  
  21.     if($x <= $xmax && $y <= $ymax
  22.         return $im;   
  23.  
  24.     if($x >= $y) { 
  25.         $newx = $xmax
  26.         $newy = $newx * $y / $x
  27.     } 
  28.     else { 
  29.         $newy = $ymax
  30.         $newx = $x / $y * $newy
  31.     }   
  32. < span> 
  33.     $im2 = imagecreatetruecolor($newx$newy); 
  34.     imagecopyresized($im2$im, 0, 0, 0, 0, floor($newx), floor($newy), $x$y); 
  35.     return $im2

21. PHP檢驗 ajax 要求

大部分的 JavaScript 架構(gòu)如 jquery,Mootools 等,在傳出 Ajax 要求時,都是會推送附加的 HTTP_X_REQUESTED_WITH 頭頂部信息內(nèi)容,頭當她們一個ajax要求,因而你能在服務端探測到 Ajax 要求。

  1. if(!emptyempty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){ 
  2.     //If AJAX Request Then 
  3. }else
    來源于申明:以上內(nèi)容一部分(包括照片、文本)來自互聯(lián)網(wǎng),若有侵權(quán)行為,請立即與本網(wǎng)站聯(lián)絡(luò)(010-57218159)。
    如沒特殊注明,文章均為酷站科技原創(chuàng),轉(zhuǎn)載請注明來自http://www.ttscar.com.cn/jianzhanzhishi/3339.html
    聯(lián)系專業(yè)的商務顧問,制定方案,專業(yè)設(shè)計,一對一咨詢及其報價詳情
    服務熱線服務熱線 18911184380
聯(lián)系我們 contact us
18911184380
0531-88903031 — 濟南分部

+

酷站科技為你提供上門/網(wǎng)站策略方案

留下聯(lián)系方式,我們將會在一個工作日內(nèi)與你聯(lián)系

隱私條款信息保護中,請放心填寫