二十一段救命的PHP代码.docx

上传人:b****3 文档编号:4383069 上传时间:2022-12-01 格式:DOCX 页数:16 大小:22.90KB
下载 相关 举报
二十一段救命的PHP代码.docx_第1页
第1页 / 共16页
二十一段救命的PHP代码.docx_第2页
第2页 / 共16页
二十一段救命的PHP代码.docx_第3页
第3页 / 共16页
二十一段救命的PHP代码.docx_第4页
第4页 / 共16页
二十一段救命的PHP代码.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

二十一段救命的PHP代码.docx

《二十一段救命的PHP代码.docx》由会员分享,可在线阅读,更多相关《二十一段救命的PHP代码.docx(16页珍藏版)》请在冰豆网上搜索。

二十一段救命的PHP代码.docx

二十一段救命的PHP代码

二十一段救命的PHP代码

   

   1.PHP可阅读随机字符串

  此代码将创建一个可阅读的字符串,使其更接近词典中的单词,实用且具有密码验证功能。

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; 

17.} 

 

  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@', $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. 

8.    $linkText = str_replace('@', '@', $linkText); 

9.    $linkText = str_replace('.', '.', $linkText); 

10.    $linkText = str_split($linkText, 5);   

11. 

12.    $part1 = '

13.    $part2 = 'ilto:'; 

14.    $part3 = '" '. $attrs .' >'; 

15.    $part4 = '';   

16. 

17.    $encoded = ''; 

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 .= '';   

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 ''.$file.'
'."\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@","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. 

4. 

5.Foo 

6.foo@ 

7. 

8. 

9.Foobar 

10.foobar@ 

11. 

12.";  

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, ''; 

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[] = '

 ' . floor( $size ) . 'px' 

16.. '" href="#" title="\'' . $tag . 

17.'\' returned a count of ' . $count . '">' 

18.. htmlspecialchars( stripslashes( $tag ) ) . ''; 

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 '

10.'&default='.$default.'&size='.$size.'&rating='.$rating.'" width="'.$size.'px" 

11.height="'.$size.'px" />'; 

12.} 

 

 

  15.PHP在字符断点处截断文字

  所谓断字(wordbreak),即一个单词可在转行时断开的地方。

这一函数将在断字处截断字符串。

1.// Original PHP code by Chirp Internet:

 .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.

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工作范文 > 行政公文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1