Reproduced, with the convenience!
1. Readable random string This code creates a readable string that is closer to the word in the dictionary, is practical and has the password validation 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 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. function Generate_rand ($len) {$c = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; Srand (Double) microtime () *1000000); for ($i =0; $i < $len; $i + +) {$rand. = $c [Rand ()%strlen ($c)]; } return $rand;} function Rand_password ($length = 8) {//password character set, you can add any character you need $chars = ' [email protected]#$%^&* ()-_ []{} <>~ ' +=,.;:/?| '; $password = "; for ($i = 0; $i < $length; $i + +) {//Here are two ways to get the characters//the first is to use subsTR intercepts any one character in $chars;//The second is any element//$password that takes a character array $chars. = substr ($chars, Mt_rand (0, strlen ($chars) –1), 1); $password. = $chars [Mt_rand (0, strlen ($chars)-1)]; } return $password; } 3. Encoded e-mail addresses can encode any e-mail address as an HTML character entity to prevent it from being collected by a spam program. function Encode_email ($email = ' [email protected] ', $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 the email address this code, in addition to verifying the e-mail address, can also choose to check 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. List Directory contents function list_files ($dir) {if (Is_dir ($dir)) {if ($handle = Opendir ($dir)) {while ($file = Readdir ($handle))!== false) {if ($file! = "." & amp;& $file! = ":" && $file! = "Thumbs.db") {echo ' <a target= ' _ Blank "href=" '. $dir. $file. ' " > '. $file. ' </a><br> '. ' \ n "; }} closedir ($handle); }}}-----------------------------function List_dir ($path) {$dp = Dir ($path); while ($fp = $DP->read ()) {if ($fp = = '. ') {}}}-----------------------------7. Parse JSON data $json _string= ' {"id": 1, "name": "foo", "email": "[email protected]", "interest": ["WordPress", "PHP"]} '; $obj =json_decode ($json _string); Echo $obj->name; Prints foo echo $obj->interest[1]; Prints php $ary = json_decode ($json _string,true); echo $ary [' id ']; 10. Get the client real IP address function getrealipaddr (){if (!emptyempty ($_server[' http_client_ip ')) {$ip =$_server[' http_client_ip ']; } elseif (!emptyempty ($_server[' http_x_forwarded_for '))//to check IP is pass from proxy { $ip =$_server[' http_x_forwarded_for ']; } else {$ip =$_server[' remote_addr '); } return $IP; } 11. Mandatory file Download function force_download ($file) {if ((Isset ($file)) && (File_exists ($file))) {Header ("Content-len Gth: ". FileSize ($file)); Header (' Content-type:application/octet-stream '); Header (' content-disposition:attachment; Filename= '. $file. ‘"‘); ReadFile ("$file"); } else {echo "No file selected"; }}12. Create a label cloud function getcloud ($data = Array (), $minFontSize = A, $maxFontSize =) {$minimumCount = min (array_values ($data)) ; $maximumCount = Max (Array_values ($data)); $spread = $maximumCount-$minimumCount; $cloudHTML = "; $cloudTags = Array (); $spread = = 0 && $spread = 1; foreach ($data as $tag = + $count) {$size = $minFontSize + ($count-$minimumCount) * ($maxFontSize-$minFontSize )/$spread; $cloudTags [] = ' <a style= ' font-size: '. Floor ($size). ' px '. ' "href=" # "title=" \ ". $tag. ' \ ' returned a count of '. $count. ' > '. Htmlspecialchars (Stripslashes ($tag)). ' </a> '; } return join ("\ n", $cloudTags). "\ n";} /** * Sample Usage * * * * $arr = array (' Actionscript ' = +, ' Adobe ' + ', ' Array ' = +, ' Background ' = 43 , ' Blur ' + +, ' Canvas ' = +, ' Class ' = ', ' Color Palette ' and ' all ', ' Crop ', ' Delimiter ' = The ' Depth ' +, ' Design ' and 8, ' Encode ', ' and ' encryption ', ' Extract ', ' + ', ' Filters ' = Echo Getcloud ($arr, 12, 36); 13. Look for the similarity of two strings Similar_text (' abcde ', ' abcdef ', $percent); Echo $percent; 16. File Zip compression function create_zip ($files = Array (), $destination = "', $overwrite =False) {if (file_exists ($destination) &&! $overwrite) {return false; } $valid _files = Array (); if (Is_array ($files)) {foreach ($files as $file) {if (file_exists ($file)) {$valid _files[] = $file; }}} if (count ($valid _files)) {$zip = new ziparchive (); if ($zip-open ($destination, $overwrite? Ziparchive:: overwrite:ziparchive:: CREATE)!== true) {return false; } foreach ($valid _files as $file) {$zip-AddFile ($file, $file); } $zip close (); return file_exists ($destination); }else{return false; }}/** * * * * * * * Example Usage * * * $files = array (' Question_20120828_211308.csv ', ' question_20120828_211326.csv '); create_ Zip ($files, ' Myzipfile.zip ', true); 17. Unzip the Zip file function Unzip_file ($file, $destination) {$zip = new ziparchive (); if ($zip->open ($file)!== TRUE) {die (' Could not open archive '); } $zip->extractto ($destination); $zip->close ();} 20. Adjust image size $imgsrc = "Http://www.nowamagic.net/images/3.jpg"; $width = 780; $height = 420;resizejpg ($IMGSRC, $IMGDST, $ width, $height), function resizejpg ($IMGSRC, $IMGDST, $imgwidth, $imgheight) {//$imgsrc JPG format image path $imgdst jpg image save file name $imgwidth to change the width $imgheight the height to be changed//get the width of the picture, the height value $arr = getimagesize ($IMGSRC); Header ("content-type:image/jpg"); $imgWidth = $imgwidth; $imgHeight = $imgheight; Create image and define colors $IMGSRC = Imagecreatefromjpeg ($IMGSRC); $image = Imagecreatetruecolor ($imgWidth, $imgHeight); Create a color Basemap imagecopyresampled ($image, $imgsrc, 0, 0, 0, 0, $imgWidth, $imgHeight, $arr [0], $arr [1]); Imagepng ($image); Imagedestroy ($image);} 21. Array to JSON format, containing Chinese characters instead of Json_encodefunction Jsonencode ($var) {switch (GetType ($var)) {case ' bool Ean ': Return $var? ' True ': ' false '; Case ' integer ': Case ' double ': return $var; Case ' resource ': Case ' string ': Return ' "'. Str_replace (Array ("\ r", "\ n", "<", ">", "&"), array (' \ r ', ' \ n ', ' \x3c ', ' \x3e ', ' \x26 '), Addslashes ($var)). ' "; Case ' array ': if (empty ($var) | | Array_keys ($var) = = = Range (0, sizeof ($var)-1)) { $output = Array (); foreach ($var as $v) {$output [] = Jsonencode ($v); } return ' ['. Implode (', ', $output). '] '; } Case ' object ': $output = Array (); foreach ($var as $k = + $v) {$output [] = Jsonencode (Strval ($k)). ': '. Jsonencode ($v); } return ' {'. Implode (', ', $output). '} '; Default:return ' null '; }}22. Delete directory and all files under it//deletE folder functionfunction deletedirectory ($dir) {if (!file_exists ($dir)) return true; if (!is_dir ($dir) | | is_link ($DIR)) return unlink ($dir); foreach (Scandir ($dir) as $item) {if ($item = = '. ' | | $item = = ' ... ') continue; if (!deletedirectory ($dir. "/" . $item) {chmod ($dir. "/" . $item, 0777); if (!deletedirectory ($dir. "/" . $item)) return false; }; } return RmDir ($dir);}
PHP Code Snippets