• <li id="0bn34"><legend id="0bn34"><th id="0bn34"></th></legend></li>

      <label id="0bn34"><mark id="0bn34"><strong id="0bn34"></strong></mark></label>
      <span id="0bn34"><optgroup id="0bn34"></optgroup></span> <label id="0bn34"><mark id="0bn34"><strong id="0bn34"></strong></mark></label>

      <rt id="0bn34"></rt>
      <span id="0bn34"><optgroup id="0bn34"><center id="0bn34"></center></optgroup></span>
      <li id="0bn34"><big id="0bn34"><listing id="0bn34"></listing></big></li>
      【北京網站制作】PHP 21段救命代碼
      • 更新時間:2025-06-07 12:49:44
      • 網站建設
      • 發布時間:2年前
      • 827

      1. PHP可閱讀隨機字符串

      此代碼將創建一個人類可讀的字符串,更接近字典中的單詞,實用且密碼有效。

      /***************

      *@length-lengthofrandomstring(mustbeamultipleof2)

      ******************/

      函數readable_random_string($length=6){

      $conso=array('b','c','d','f','g','h','j','k','l',

      'm','n','p','r','s','t','v','w','x','y','z');

      $vocal=array('a','e','i',

      an>"o","u");?
    1. ????$password="";?
    2. ????srand?((double)microtime()*1000000);?
    3. ????$max?=?$length/2;?
    4. ????for($i=1;?$i<=$max;?$i++)?
    5. ????{?
    6. ????$password.=$conso[rand(0,19)];?
    7. ????$password.=$vocal[rand(0,4)];?
    8. ????}?
    9. ????return?$password;?
    10. }?
    11. 2. PHP生成一個隨機字符串

        如果不需要可閱讀的字符串,使用此函數替代,即可創建一個隨機字符串,作為用戶的隨機密碼等。

      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 字符實體,以防止被垃圾郵件程序收集。

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

      4. PHP驗證郵件地址

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

      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))?
      4. ????????if($test_mx)?
      5. ????????{?
      6. ????????????list($username,?$domain)?=?split("@",?$email);?
      7. ????????????return?getmxrr($domain,?$mxrecords);?
      8. ????????}?
      9. ????????else?
      10. ????????????return?true;?
      11. ????else?
      12. ????????return?false;?
      13. }?

      5. PHP列出目錄內容

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

      6. PHP銷毀目錄

        刪除一個目錄,包括它的內容。

      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 數據

        與大多數流行的 Web 服務如 twitter 通過開放 API 來提供數據一樣,它總是能夠知道如何解析 API 數據的各種傳送格式,包括 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 數據

      1. //xml?string?
      2. $xml_string="<?xml?version='1.0'?>?
      3. <users>?
      4. <user?id='398'>?
      5. <name>Foo</name>?
      6. <email>foo@bar.com</name>?
      7. </user>?
      8. <user?id='867'>?
      9. <name>Foobar</name>?
      10. <email>foobar@foo.com</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. {?
      20. //access?attribute?
      21. echo?$user['id'],?'?';?
      22. //subnodes?are?accessed?by?->?operator?
      23. echo?$user->name,?'?';?
      24. echo?$user->email,?'<br?/>';?
      25. }?

      9. PHP創建日志縮略名

        創建用戶友好的日志縮略名。

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

      10. PHP獲取客戶端真實 IP 地址

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

      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強制性文件下載

        為用戶提供強制性的文件下載功能。

      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. }?else?{?
      12. echo?"No?file?selected";?
      13. }?
      14. }?

      12. PHP創建標簽云

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

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

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

      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. {?
      9. echo?'<img?src="http://www.gravatar.com/avatar.php?gravatar_id='.md5($email).?
      10. '&default='.$default.'&size='.$size.'&rating='.$rating.'"?width="'.$size.'px"?
      11. height="'.$size.'px"?/>';?
      12. }?

      15. PHP在字符斷點處截斷文字

        所謂斷字 (word break),即一個單詞可在轉行時斷開的地方。這一函數將在斷字處截斷字符串。

      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. }?
      13. }?
      14. return?$string;?
      15. }?
      16. /*****?Example?****/?
      17. $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. }?
      15. }?
      16. }?
      17. //if?we?have?good?files...?
      18. if(count($valid_files))?{?
      19. //create?the?archive?
      20. $zip?=?new?ZipArchive();?
      21. if($zip->open($destination,$overwrite???ZIPARCHIVE::OVERWRITE?:?ZIPARCHIVE::CREATE)?!==?true)?{?
      22. return?false;?
      23. }?
      24. //add?the?files?
      25. foreach($valid_files?as?$file)?{?
      26. $zip->addFile($file,$file);?
      27. }?
      28. //debug?
      29. //echo?'The?zip?archive?contains?',$zip->numFiles,'?files?with?a?status?of?',$zip->status;??
      30. ?
      31. //close?the?zip?--?done!?
      32. $zip->close();??
      33. ?
      34. //check?to?make?sure?the?file?exists?
      35. return?file_exists($destination);?
      36. }?
      37. else?
      38. {?
      39. return?false;?
      40. }?
      41. }?
      42. /*****?Example?Usage?***/?
      43. $files=array('file1.jpg',?'file2.jpg',?'file3.gif');?
      44. 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. PHP為 URL 地址預設 http 字符串

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

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

      19. PHP將網址字符串轉換成超級鏈接

        該函數將 URL 和 E-mail 地址字符串轉換為可點擊的超級鏈接。

      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;?
      10. }?

      20. PHP調整圖像尺寸

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

      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;?
      37. }?

      21. PHP檢測 ajax 請求

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

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

      我們專注高端建站,小程序開發、軟件系統定制開發、BUG修復、物聯網開發、各類API接口對接開發等。十余年開發經驗,每一個項目承諾做到滿意為止,多一次對比,一定讓您多一份收獲!

      本文章出于推來客官網,轉載請表明原文地址:https://www.tlkjt.com/web/13834.html
      推薦文章

      在線客服

      掃碼聯系客服

      3985758

      回到頂部

      亚洲国产精品成人午夜在线观看| 国产欧美日韩综合精品一区二区| 五月天婷婷在线播放| 欧美日本高清在线不卡区| 国产成人高清视频| 99久久99久久精品国产片果冻 | 亚洲成色www久久网站| 沈婷婷小雷第三次| 免费看日b视频| 精品久久精品久久| 国产一级片观看| 色综合久久久无码中文字幕波多| 国产成社区在线视频观看| 青青免费在线视频| 国产成人一区二区三区精品久久| **真实毛片免费观看| 国产精品无码久久综合网| 99久久超碰中文字幕伊人| 在线观看网址入口2020国产| 一本之道无吗一二三区| 小坏蛋轻点阿受不了漫画| 中文字幕电影在线| 成人片黄网站a毛片免费| 久久久久久国产精品免费免费| 日本bbwbbwbbw| 久久精品亚洲视频| 日本一道dvd在线播放| 久久99精品久久久久久青青日本| 成年无码av片在线| 久久久久久不卡| 成人性生活免费看| 一本久久a久久精品vr综合| 国产超级乱淫视频播放| 99久久婷婷国产综合亚洲| 国产真实伦视频在线视频| 高清性色生活片欧美在线| 国产手机精品一区二区| 羞羞答答xxdd影院欧美| 国产69精品久久久久777| 精品国产免费一区二区三区| 啊灬啊灬啊灬快灬深用力点|