Some practical custom functions of PHP are collected. Although PHP comes with powerful function libraries, in many cases, we also have to write custom functions to implement some functions and requirements. Some useful functions are collected below. Although PHP comes with powerful function libraries, we also have to write custom functions to implement some functions and requirements in many daily situations. The following are some useful custom functions that can solve common requirements, such as converting URL strings into hyperlink, listing directory content, and verifying email addresses, this page can be added to favorites for later reading ~
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 = ''; $ part4 =''; $ encoded ='