Share 21 common PHP function code segments

Source: Internet
Author: User
Share the 21 frequently used PHP function code segments. The following describes the 21 function code segments that are frequently used in PHP Development. when we use them, we can use them directly. 1. PHP readable random string this code creates a readable string that is closer to the word in the dictionary and is useful and has the password verification function. /*************** @ Length-lengt share the 21 frequently used PHP function code segments

The following describes the 21 function code segments that are frequently used in PHP Development. when we use them, we can use them directly.

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.

/**************
* @ Length-length of random string (must be a multiple of 2)
**************/
Function 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", "o", "u ");?
?? ? $ Password = "";?
??? ? Srand (double) microtime () * 1000000 );?
?? ? $ Max = $ length/2 ;?
?? ? For ($ I = 1; $ I <= $ max; $ I ++ ){?
?? ??? ? $ Password. = $ conso [rand (0, 19)];?
?? ??? ? $ Password. = $ vocal [rand (0, 4)];?
?? ?} ?
?? ? 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.
/*************?
* @ L-length of random string?
*/?
Function? Generate_rand ($ l ){?
?? ? $ C = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ";?
?? ? Srand (double) microtime () * 1000000 );
??? ? For ($ I = 0 ;? $ I <$ l ;? $ I ++ ){?
?? ??? ? $ Rand. =? $ C [rand () % strlen ($ c)];?
?? ?} ?
?? ? 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.
Function? Encode_email ($ email = 'info @ domain.com ',? $ LinkText = 'contact us', $ attrs? = 'Class = "emailencoder "'){?
?? ? // Remplazar aroba y puntos?
?? ? $ Email? =? Str_replace ('@', '@', $ email );?
?? ? $ Email? =? Str_replace ('.', '.', $ email );?
?? ? $ Email? =? Str_split ($ email, 5 );?
?? ? $ LinkText = str_replace ('@', '@', $ linkText );?
?? ??? ? $ LinkText? =? Str_replace ('.', '.', $ linkText );?
?? ? $ LinkText? =? Str_split ($ linkText, 5 );?
?? ? $ Part1? = '?? ? $ Part2? = 'Ilto :';?
??? ? $ Part3? = '"'. $ Attrs. '> ';?
?? ? $ Part4? = '';?
?? ? $ Encoded? ='

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.