Share frequently used 21 PHP function code segments (I) (1)

Source: Internet
Author: User

The following describes howPHPThe 21 function code segments that are frequently used during development can be directly used when we use them.

1. PHP can read random strings

This Code creates a readable string that is closer to a word in the dictionary and is practical and password-verified.

 
 
  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 generates a random string

If you do not need readable strings, use this function instead to create a random string as your random password.

 
 
  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-encoded email address

This code can be used to encode any email address as an html character entity to prevent being collected by spam programs.

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

4. PHP verification email address

Email verification may be the most common web form verification. In addition to verifying the email address, this code can also choose to check MX records in the DNS to which the email domain belongs, making email verification more powerful.

 
 
  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;  

5. PHP listing directory content

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

1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.