Code Rollup for 21 functions commonly used in PHP development website

Source: Internet
Author: User
Tags foreach array json php code rand web services domain mx record

PHP is currently the most widely used web-based programming language, driving millions of web sites, including some large sites such as Facebook. This collection of 21 practical and convenient snippets of PHP code is helpful for each type of PHP developer.

1. Can read random string

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

/**************
* @length-length of random string (must 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. Generate a random string

If you don't need a readable string, use this function instead to create a random string that acts as a random password for the user.

/*************
*@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. Encoded Email Address

With this code, you can encode any e-mail address as an HTML character entity to prevent it from being collected by the Junk Mail 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 = ' <a href= ' ma ';
$part 2 = ' Ilto: ';
$part 3 = ' "'. $attrs. ' > ';
$part 4 = ' </a> ';
$encoded = ' <script type= ' text/javascript ' > ';
$encoded. = "document.write (' $part 1 ');";
$encoded. = "document.write (' $part 2 ');";
foreach ($email as $e)
{
$encoded. = "document.write (' $e ');";
}
$encoded. = "document.write (' $part 3 ');";
foreach ($linkText as $l)
{
$encoded. = "document.write (' $l ');";
}
$encoded. = "document.write (' $part 4 ');";
$encoded. = ' </script> ';

return $encoded;
}

4. Verify email address

E-mail authentication is perhaps the most common form of Web page verification, which, in addition to verifying e-mail addresses, optionally checks the MX record in the DNS of the mail domain to make the message validation 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. Listing contents

function List_files ($dir)
{
if (Is_dir ($dir))
{
if ($handle = Opendir ($dir))
{
while (($file = Readdir ($handle))!== false)
{
if ($file!= "." && $file!= "..." && $file!= "Thumbs.db")
{
Echo ' <a target= ' _blank ' href= '. $dir. $file. ' " > '. $file. ' </a><br> '. ' \ n ";
}
}
Closedir ($handle);
}
}
}

6. Destruction of the catalogue

Deletes a directory, including its contents.

/*****
* @dir-directory to destroy
* @virtual [optional]-whether a virtual directory
*/
function Destroydir ($dir, $virtual = False)
{
$ds = Directory_separator;
$dir = $virtual? Realpath ($dir): $dir;
$dir = substr ($dir,-1) = = $ds? substr ($dir, 0,-1): $dir;
if (Is_dir ($dir) && $handle = Opendir ($dir))
{
while ($file = Readdir ($handle))
{
if ($file = = '. ' $file = = '. ')
{
Continue
}
ElseIf (Is_dir ($dir. $ds. $file))
{
Destroydir ($dir. $ds. $file);
}
Else
{
Unlink ($dir. $ds. $file);
}
}
Closedir ($handle);
RmDir ($dir);
return true;
}
Else
{
return false;
}
}

7. Parse JSON data

As with most popular WEB services such as Twitter, which provides data through an open API, it always knows how to parse the various delivery formats for API data, including Json,xml, and so on.

$json _string= ' {"id": 1, "name": "foo", "email": "foo@foobar.com", "Interest": ["WordPress", "PHP"]} ';
$obj =json_decode ($json _string);
Echo $obj->name; Prints Foo
Echo $obj->interest[1]; Prints PHP



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.