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.
- /**************
- * @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 a readable string, use this function instead to create a random string, as a random password for the user, and so on.
- /*************
- *@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 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.
- 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);
- $part 1 = '
- $part 2 = ' Ilto: ';
- $part 3 = ' "'. $attrs . ' > ';
- $part 4 = ";
- $encoded = ";
- return $encoded;
- }
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.
- function is_valid_email ($email, $test _mx = False)
- {
- if (eregi("^ ([_a-z0-9-]+) (. [ _a-z0-9-]+) *@ ([a-z0-9-]+) (. [ a-z0-9-]+) * (. [ a-z]{2,4}) $ ", $email))
- if ($test _mx)
- {
- list ($username, $domain) = Split ("@", $email );
- return getmxrr($domain, $mxrecords);
- }
- Else
- return true;
- Else
- return false;
- }
5. PHP Lists directory contents
- function list_files ($dir)
- {
- if (is_dir($dir))
- {
- if ($handle = Opendir ($dir))
- {
- while (($file = Readdir ($handle))!== false)
- {
- if ($file ! = "." && $file ! = ":" && $file c14>! = "Thumbs.db")
- {
- Echo '$dir. $file . ' " > '. $file . '
’.” n ";
- }
- }
- Closedir ($handle);
- }
- }
- }
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 ...