Share 21 PHP function code Snippets (UP) (1) _php Tutorials

Source: Internet
Author: User
Tags vars
The following is an introduction to the PHPIn the development, often use of 21 function code snippets, when we use, it can be used directly.

1. PHP can read random strings

This code creates a readable string that is closer to the word in the dictionary, and is practical and has password validation capabilities.

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

If you do not need a readable string, use this function instead to create a random string, as a random password for the user, and so on.

 
 
  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 encoded e-mail address

With this code, any e-mail address can be encoded as an HTML character entity to prevent it from being collected by the spam program.

 
 
  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 (' @ ', ' @ ', $email);
  6. $email = Str_replace ('. ', '. ', $email);
  7. $email = Str_split ($email, 5);
  8. $linkText = Str_replace (' @ ', ' @ ', $linkText);
  9. $linkText = Str_replace ('. ', '. ', $linkText);
  10. $linkText = Str_split ($linkText, 5);
  11. $part 1 = '
  12. $part 2 = ' Ilto: ';
  13. $part 3 = ' "'. $attrs . ' > ';
  14. $part 4 = ";
  15. $encoded = ";
  16. return $encoded;
  17. }

4. PHP Verification Email Address

e-mail validation is perhaps the most common form validation in Web pages, which, in addition to verifying e-mail addresses, also has the option of checking the MX records in the DNS that the mail domain belongs to, making the message verification feature 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;
  13. }

5. PHP Lists directory contents

 
 
  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 c14>! = "Thumbs.db")
  10. {
  11. Echo '$dir. $file . ' " > '. $file . '
    ’.” n ";
  12. }
  13. }
  14. Closedir ($handle);
  15. }
  16. }
  17. }

1

http://www.bkjia.com/PHPjc/445762.html www.bkjia.com true http://www.bkjia.com/PHPjc/445762.html techarticle The following is, in PHP development, often used in the 21 function code snippets, when we use, it can be used directly. 1. PHP can read random string This code will create a ...

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