日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Web開發(fā)者必備:21個超實(shí)用PHP代碼

   1. PHP可閱讀隨機(jī)字符串

站在用戶的角度思考問題,與客戶深入溝通,找到峽江網(wǎng)站設(shè)計與峽江網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站設(shè)計制作、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、國際域名空間、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋峽江地區(qū)。

   此代碼將創(chuàng)建一個可閱讀的字符串,使其更接近詞典中的單詞,實(shí)用且具有密碼驗(yàn)證功能。

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

2. PHP生成一個隨機(jī)字符串

   如果不需要可閱讀的字符串,使用此函數(shù)替代,即可創(chuàng)建一個隨機(jī)字符串,作為用戶的隨機(jī)密碼等。

 
 
 
  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;  
  11.  }  

3. PHP編碼電子郵件地址

   使用此代碼,可以將任何電子郵件地址編碼為 html 字符實(shí)體,以防止被垃圾郵件程序收集。

 
 
 
  1. function encode_email($email='info@domain.com', $linkText='Contact Us', $attrs ='class="emailencoder"' )  
  2. {   
  3. // remplazar aroba y puntos   
  4. $email = str_replace('@', '@', $email);   
  5. $email = str_replace('.', '.', $email);   
  6. $email = str_split($email, 5);   
  7. $linkText = str_replace('@', '@', $linkText);   
  8. $linkText = str_replace('.', '.', $linkText);   
  9. $linkText = str_split($linkText, 5); $part1 = ' part2  = 'ilto:'; $part3 = '" '. $attrs .' >';   
  10. $part4 = '';   
  11. $encoded = ' ' ;   
  12. $encoded .= "document.write('$part1');";   
  13. $encoded .= "document.write('$part2');";   
  14. foreach($email as $e)   
  15. {   
  16. $encoded .= "document.write('$e');";  
  17.  }   
  18. $encoded .= "document.write('$part3');";  
  19.  foreach($linkText as $l)   
  20. {  
  21.  $encoded .= "document.write('$l');";   
  22. }   
  23. $encoded .= "document.write('$part4');";   
  24. $encoded .= ''; return $encoded;}  

   4. PHP驗(yàn)證郵件地址

   電子郵件驗(yàn)證也許是中最常用的網(wǎng)頁表單驗(yàn)證,此代碼除了驗(yàn)證電子郵件地址,也可以選擇檢查郵件域所屬 DNS 中的 MX 記錄,使郵件驗(yàn)證功能更加強(qiáng)大。

 
 
 
  1. function is_valid_email($email, $test_mx = false)  
  2. {  
  3.  if(eregi("^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) 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;  
  12. }  

#p#

   5. PHP列出目錄內(nèi)容

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

  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. {   
  7. $ds = DIRECTORY_SEPARATOR;   
  8. $dir = $virtual ? realpath($dir) : $dir;   
  9. $dir = substr($dir, -1) == $ds ? substr($dir, 0, -1) : $dir;   
  10. if (is_dir($dir) && $handle = opendir($dir))  
  11.  {  
  12.  while ($file = readdir($handle))   
  13. {   
  14. if ($file == '.' || $file == '..')   
  15. {  
  16.  continue;  
  17.  }   
  18. elseif (is_dir($dir.$ds.$file))   
  19. {   
  20. destroyDir($dir.$ds.$file);  
  21.  }   
  22. else   
  23. {  
  24.  unlink($dir.$ds.$file);   
  25. }  
  26.  }   
  27. closedir($handle);  
  28.  rmdir($dir);   
  29. return true;   
  30. }   
  31. else  
  32.  {   
  33. return false;  
  34.  }  
  35. }  

7. PHP解析 JSON 數(shù)據(jù)

   與大多數(shù)流行的 Web 服務(wù)如 twitter 通過開放 API 來提供數(shù)據(jù)一樣,它總是能夠知道如何解析 API 數(shù)據(jù)的各種傳送格式,包括 JSON,XML 等等。

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

   8. PHP解析 XML 數(shù)據(jù)

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

#p#
9. PHP創(chuàng)建日志縮略名

創(chuàng)建用戶友好的日志縮略名。

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

  10. PHP獲取客戶端真實(shí) IP 地址

   該函數(shù)將獲取用戶的真實(shí) IP 地址,即便他使用代理服務(wù)器。

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

   11. PHP強(qiáng)制性文件下載

   為用戶提供強(qiáng)制性的文件下載功能。

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

12. PHP創(chuàng)建標(biāo)簽云

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

   13. PHP尋找兩個字符串的相似性

   PHP 提供了一個極少使用的 similar_text 函數(shù),但此函數(shù)非常有用,用于比較兩個字符串并返回相似程度的百分比。

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

   14. PHP在應(yīng)用程序中使用 Gravatar 通用頭像

   隨著 WordPress 越來越普及,Gravatar 也隨之流行。由于 Gravatar 提供了易于使用的 API,將其納入應(yīng)用程序也變得十分方便。

 
 
 
  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. {  
  9.  echo '
  10. height="'.$size.'px" />';  
  11. }  
  12.  

#p#

   15. PHP在字符斷點(diǎn)處截斷文字

   所謂斷字 (word break),即一個單詞可在轉(zhuǎn)行時斷開的地方。這一函數(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. // is $break present between $limit and the end of the string?   
  8. if(false !== ($breakpoint = strpos($string, $break, $limit))) {  
  9.  if($breakpoint < strlen($string) - 1) {   
  10. $string = substr($string, 0, $breakpoint) . $pad;  
  11.  }  
  12.  }  
  13.  return $string;  
  14. }  
  15. /***** Example ****/  
  16. $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 $valid_files = array();   
  6. //if files were passed in...   
  7. if(is_array($files)) {   
  8. //cycle through each file  
  9. foreach($files as $file) {  
  10.  //make sure the file exists  
  11.  if(file_exists($file)) {   
  12. $valid_files[] = $file;  
  13.  }   
  14. }   
  15. }  
  16. //if we have good files...   
  17. if(count($valid_files)) {  
  18.  //create the archive   
  19. $zip = new ZipArchive();   
  20. if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { return false;  
  21.  }   
  22. //add the files   
  23. foreach($valid_files as $file) {   
  24. $zip->addFile($file,$file);  
  25.  }   
  26. //debug //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;   
  27. //close the zip -- done!  
  28.  $zip->close();   
  29. //check to make sure the file exists  
  30.  return file_exists($destination);  
  31.  }   
  32. else   
  33. {  
  34.  return false;   
  35. }  
  36. }  
  37. /***** Example Usage ***/  
  38. $files=array('file1.jpg', 'file2.jpg', 'file3.gif');  
  39. 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. }  
  12. // extract contents to destination directory  
  13. $zip->extractTo($destination);  
  14. // close archive  
  15. $zip->close();  
  16. echo 'Archive extracted to directory';  
  17. }  
  18.  

#p#

    18. PHP為 URL 地址預(yù)設(shè) http 字符串

    有時需要接受一些表單中的網(wǎng)址輸入,但用戶很少添加 http:// 字段,此代碼將為網(wǎng)址添加該字段。

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

   19. PHP將網(wǎng)址字符串轉(zhuǎn)換成超級鏈接

   該函數(shù)將 URL 和 E-mail 地址字符串轉(zhuǎn)換為可點(diǎn)擊的超級鏈接。

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

   20. PHP調(diào)整圖像尺寸

   創(chuàng)建圖像縮略圖需要許多時間,此代碼將有助于了解縮略圖的邏輯。

 
 
 
  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. {  
  9. $ext = explode(".", $filename);  
  10. $ext = $ext[count($ext)-1];   
  11.  
  12. if($ext == "jpg" || $ext == "jpeg")  
  13. $im = imagecreatefromjpeg($tmpname);  
  14. elseif($ext == "png")  
  15. $im = imagecreatefrompng($tmpname);  
  16. elseif($ext == "gif")  
  17. $im = imagecreatefromgif($tmpname);   
  18.  
  19. $x = imagesx($im);  
  20. $y = imagesy($im);   
  21.  
  22. if($x <= $xmax && $y <= $ymax)  
  23. return $im;   
  24.  
  25. if($x >= $y) {  
  26. $newx = $xmax;  
  27. $newy = $newx * $y / $x;  
  28. }  
  29. else {  
  30. $newy = $ymax;  
  31. $newx = $x / $y * $newy;  
  32. }   
  33.  
  34. $im2 = imagecreatetruecolor($newx, $newy);  
  35. imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y);  
  36. return $im2;  

21. PHP檢測 ajax 請求

    大多數(shù)的 JavaScript 框架如 jquery,Mootools 等,在發(fā)出 Ajax 請求時,都會發(fā)送額外的 HTTP_X_REQUESTED_WITH 頭部信息,頭當(dāng)他們一個ajax請求,因此你可以在服務(wù)器端偵測到 Ajax 請求。

 
 
 
  1. if(!emptyempty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){  
  2. //If AJAX Request Then  
  3. }else{  
  4. //something else  
  5. }  

【編輯推薦】:

  1. PHP+XML+jQuery實(shí)現(xiàn)即時功能
  2. 詳解PHP內(nèi)存池中的存儲層

文章名稱:Web開發(fā)者必備:21個超實(shí)用PHP代碼
URL鏈接:http://www.dlmjj.cn/article/dhhshsp.html