日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

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

新聞中心

這里有您想知道的互聯(lián)網營銷解決方案
分享經常用到的21個PHP函數(shù)代碼段(下)

下面介紹的是,在PHP開發(fā)中,經常用到的21個函數(shù)代碼段,當我們用到的時候,就可以直接用了。

成都創(chuàng)新互聯(lián)一直通過網站建設和網站營銷幫助企業(yè)獲得更多客戶資源。 以"深度挖掘,量身打造,注重實效"的一站式服務,以成都網站設計、網站建設、移動互聯(lián)產品、成都全網營銷服務為核心業(yè)務。十余年網站制作的經驗,使用新網站建設技術,全新開發(fā)出的標準網站,不但價格便宜而且實用、靈活,特別適合中小公司網站制作。網站管理系統(tǒng)簡單易用,維護方便,您可以完全操作網站資料,是中小公司快速網站建設的選擇。

接上一篇,分享經常用到的21個PHP函數(shù)代碼段(上)

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

 
 
 
  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. $spread == 0 && $spread = 1;
  9. foreach( $data as $tag => $count )
  10. {
  11. $size = $minFontSize + ( $count – $minimumCount )
  12. * ( $maxFontSize – $minFontSize ) / $spread;
  13. $cloudTags[] = ‘
  14. . ‘” href=”#” title=”\” . $tag .
  15. ‘\’ returned a count of ‘ . $count . ‘”>’
  16. . htmlspecialchars( stripslashes( $tag ) ) . ‘’;
  17. }
  18. return join( “\n”, $cloudTags ) . “\n”;
  19. }
  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尋找兩個字符串的相似性

PHP 提供了一個極少使用的 similar_text 函數(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. {
  9. echo ‘
  10. ‘&default=’.$default.’&size=’.$size.’&rating=’.$rating.’” width=”‘.$size.’px”
  11. height=”‘.$size.’px” />’;
  12. }

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

所謂斷字 (word break),即一個單詞可在轉行時斷開的地方。這一函數(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
  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. //close the zip — done!
  31. $zip->close();
  32. //check to make sure the file exists
  33. return file_exists($destination);
  34. }
  35. else
  36. {
  37. return false;
  38. }
  39. }
  40. /***** Example Usage ***/
  41. $files=array(‘file1.jpg’, ‘file2.jpg’, ‘file3.gif’);
  42. create_zip($files, ‘myzipfile.zip’, true);

#p#

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將網址字符串轉換成超級鏈接

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

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

20. PHP調整圖像尺寸

創(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. 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. $x = imagesx($im);
  18. $y = imagesy($im);
  19. if($x <= $xmax && $y <= $ymax)
  20. return $im;
  21. if($x >= $y) {
  22. $newx = $xmax;
  23. $newy = $newx * $y / $x;
  24. }
  25. else {
  26. $newy = $ymax;
  27. $newx = $x / $y * $newy;
  28. }
  29. $im2 = imagecreatetruecolor($newx, $newy);
  30. imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y);
  31. return $im2;
  32. }

21. PHP檢測 ajax 請求

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

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

到這,21個經常用到的PHP函數(shù)代碼段,就大家介紹完了。希望對你有幫助。


分享文章:分享經常用到的21個PHP函數(shù)代碼段(下)
瀏覽路徑:http://m.5511xx.com/article/cdspeoe.html