Home > Article > Backend Development > Share 21 frequently used PHP function code snippets (Part 1) (1)_PHP Tutorial
The following introduces 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 will create a readable string, making it closer to the word in the dictionary, practical and cryptographic Verification function.
<ol class="dp-c"> <li class="alt"><span><span class="comment">/************** </span> </span></li> <li><span><span class="comment">*@length – length of random string (must be a multiple of 2) </span> </span></li> <li class="alt"><span><span class="comment">**************/</span><span> </span></span></li> <li> <span class="keyword">function</span><span> readable_random_string(</span><span class="vars">$length</span><span> = 6){ </span> </li> <li class="alt"> <span class="vars">$conso</span><span>=</span><span class="keyword">array</span><span>(“b”,”c”,”d”,”f”,”g”,”h”,”j”,”k”,”l”, </span> </li> <li><span>“m”,”n”,”p”,”r”,”s”,”t”,”v”,”w”,”x”,”y”,”z”); </span></li> <li class="alt"> <span class="vars">$vocal</span><span>=</span><span class="keyword">array</span><span>(“a”,”e”,”i”,”o”,”u”); </span> </li> <li> <span class="vars">$password</span><span>=”"; </span> </li> <li class="alt"><span>srand ((double)microtime()*1000000); </span></li> <li> <span class="vars">$max</span><span> = </span><span class="vars">$length</span><span>/2; </span> </li> <li class="alt"> <span class="keyword">for</span><span>(</span><span class="vars">$i</span><span>=1; </span><span class="vars">$i</span><span><=</span><span class="vars">$max</span><span>; </span><span class="vars">$i</span><span>++) </span></li><li><span>{ </span></li><li class="alt"><span class="vars">$password</span><span>.=</span><span class="vars">$conso</span><span>[rand(0,19)]; </span></li><li><span class="vars">$password</span><span>.=</span><span class="vars">$vocal</span><span>[rand(0,4)]; </span></li><li class="alt"><span>} </span></li><li><span class="keyword">return</span><span> </span><span class="vars">$password</span><span>; </span></li><li class="alt"><span>} </span></li></ol>
2. PHP generates a random string
If a readable string is not required, Use this function instead to create a random string that can be used as a random password for a user, etc.
<ol class="dp-c"><li class="alt"><span><span class="comment">/************* </span> </span></li><li><span><span class="comment">*@l – length of random string </span> </span></li><li class="alt"><span><span class="comment">*/</span><span> </span></span></li><li><span class="keyword">function</span><span> generate_rand(</span><span class="vars">$l</span><span>){ </span></li><li class="alt"><span class="vars">$c</span><span>= “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789″; </span></li><li><span>srand((double)microtime()*1000000); </span></li><li class="alt"><span class="keyword">for</span><span>(</span><span class="vars">$i</span><span>=0; </span><span class="vars">$i</span><span><</span><span class="vars">$l</span><span>; </span><span class="vars">$i</span><span>++) { </span></li><li><span class="vars">$rand</span><span>.= </span><span class="vars">$c</span><span>[rand()%</span><span class="func">strlen</span><span>(</span><span class="vars">$c</span><span>)]; </span></li><li class="alt"><span>} </span></li><li><span class="keyword">return</span><span> </span><span class="vars">$rand</span><span>; </span></li><li class="alt"><span>} </span></li></ol>
3. PHP encoding email address
Using this code, any email address can be Encoded as html character entities to prevent collection by spam programs.
<ol class="dp-c"><li class="alt"><span><span class="keyword">function</span><span> encode_email(</span><span class="vars">$email</span><span>=’info@domain.com’, </span><span class="vars">$linkText</span><span>=’Contact Us’, </span></span></li><li class="alt"><span><span class="vars">$attrs</span><span> =’</span><span class="keyword">class</span><span>=”emailencoder”‘ ) </span></span></li><li><span>{ </span></li><li class="alt"><span class="comment">// remplazar aroba y puntos </span><span> </span></li><li><span class="vars">$email</span><span> = </span><span class="func">str_replace</span><span>(‘@’, ‘@’, </span><span class="vars">$email</span><span>); </span></li><li class="alt"><span class="vars">$email</span><span> = </span><span class="func">str_replace</span><span>(‘.’, ‘.’, </span><span class="vars">$email</span><span>); </span></li><li><span class="vars">$email</span><span> = </span><span class="func">str_split</span><span>(</span><span class="vars">$email</span><span>, 5); </span></li><li class="alt"><span class="vars">$linkText</span><span> = </span><span class="func">str_replace</span><span>(‘@’, ‘@’, </span><span class="vars">$linkText</span><span>); </span></li><li><span class="vars">$linkText</span><span> = </span><span class="func">str_replace</span><span>(‘.’, ‘.’, </span><span class="vars">$linkText</span><span>); </span></li><li class="alt"><span class="vars">$linkText</span><span> = </span><span class="func">str_split</span><span>(</span><span class="vars">$linkText</span><span>, 5); </span></li><li><span class="vars">$part1</span><span> = ‘<a href=”ma’; </span></li><li class="alt"><span class="vars">$part2</span><span> = ‘ilto:’; </span></li><li><span class="vars">$part3</span><span> = ‘” ‘. </span><span class="vars">$attrs</span><span> .’ >’; </span> </li> <li class="alt"> <span class="vars">$part4</span><span> = ‘</a>’; </span> </li> <li> <span class="vars">$encoded</span><span> = ‘<script type=”text/javascript”>’; </span> </li> <li class="alt"> <span class="vars">$encoded</span><span> .= “document.write(‘</span><span class="vars">$part1</span><span>′);”; </span> </li> <li> <span class="vars">$encoded</span><span> .= “document.write(‘</span><span class="vars">$part2</span><span>′);”; </span> </li> <li class="alt"> <span class="keyword">foreach</span><span>(</span><span class="vars">$email</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$e</span><span>) </span> </li> <li><span>{ </span></li> <li class="alt"> <span class="vars">$encoded</span><span> .= “document.write(‘</span><span class="vars">$e</span><span>’);”; </span> </li> <li><span>} </span></li> <li class="alt"> <span class="vars">$encoded</span><span> .= “document.write(‘</span><span class="vars">$part3</span><span>′);”; </span> </li> <li> <span class="keyword">foreach</span><span>(</span><span class="vars">$linkText</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$l</span><span>) </span> </li> <li class="alt"><span>{ </span></li> <li> <span class="vars">$encoded</span><span> .= “document.write(‘</span><span class="vars">$l</span><span>’);”; </span> </li> <li class="alt"><span>} </span></li> <li> <span class="vars">$encoded</span><span> .= “document.write(‘</span><span class="vars">$part4</span><span>′);”; </span> </li> <li class="alt"> <span class="vars">$encoded</span><span> .= ‘</script>’; </span> </li> <li> <span class="keyword">return</span><span> </span><span class="vars">$encoded</span><span>; </span> </li> <li class="alt"><span>} </span></li> </ol>
4. PHP verification email address
Email verification is perhaps the most commonly used web form. Verification, in addition to verifying the email address, this code can also choose to check the MX record in the DNS to which the email domain belongs, making the email verification function more powerful.
<ol class="dp-c"> <li class="alt"><span><span class="keyword">function</span><span> is_valid_email(</span><span class="vars">$email</span><span>, </span><span class="vars">$test_mx</span><span> = false) </span></span></li> <li><span>{ </span></li> <li class="alt"> <span class="keyword">if</span><span>(</span><span class="func">eregi</span><span>(“^([_a-z0-9-]+)(.[_a-z0-9-]+)*@([a-z0-9-]+)(.[a-z0-9-]+)*(.[a-z]{2,4})$”, </span><span class="vars">$email</span><span>)) </span> </li> <li> <span class="keyword">if</span><span>(</span><span class="vars">$test_mx</span><span>) </span> </li> <li class="alt"><span>{ </span></li> <li> <span>list(</span><span class="vars">$username</span><span>, </span><span class="vars">$domain</span><span>) = split(“@”, </span><span class="vars">$email</span><span>); </span> </li> <li class="alt"> <span class="keyword">return</span><span> </span><span class="func">getmxrr</span><span>(</span><span class="vars">$domain</span><span>, </span><span class="vars">$mxrecords</span><span>); </span> </li> <li><span>} </span></li> <li class="alt"> <span class="keyword">else</span><span> </span> </li> <li> <span class="keyword">return</span><span> true; </span> </li> <li class="alt"> <span class="keyword">else</span><span> </span> </li> <li> <span class="keyword">return</span><span> false; </span> </li> <li class="alt"><span>} </span></li> </ol>
5. PHP lists directory contents
<ol class="dp-c"> <li class="alt"><span><span class="keyword">function</span><span> list_files(</span><span class="vars">$dir</span><span>) </span></span></li> <li><span>{ </span></li> <li class="alt"> <span class="keyword">if</span><span>(</span><span class="func">is_dir</span><span>(</span><span class="vars">$dir</span><span>)) </span> </li> <li><span>{ </span></li> <li class="alt"> <span class="keyword">if</span><span>(</span><span class="vars">$handle</span><span> = opendir(</span><span class="vars">$dir</span><span>)) </span> </li> <li><span>{ </span></li> <li class="alt"> <span class="keyword">while</span><span>((</span><span class="vars">$file</span><span> = readdir(</span><span class="vars">$handle</span><span>)) !== false) </span> </li> <li><span>{ </span></li> <li class="alt"> <span class="keyword">if</span><span>(</span><span class="vars">$file</span><span> != “.” && </span><span class="vars">$file</span><span> != “..” && </span><span class="vars">$file</span><span> != “Thumbs.db”) </span> </li> <li><span>{ </span></li> <li class="alt"> <span class="func">echo</span><span> ‘<a target=”_blank” href=”‘.</span><span class="vars">$dir</span><span>.</span><span class="vars">$file</span><span>.’”>’.</span><span class="vars">$file</span><span>.’</a><br>’.”n”; </span> </li> <li><span>} </span></li> <li class="alt"><span>} </span></li> <li> <span class="func">closedir</span><span>(</span><span class="vars">$handle</span><span>); </span> </li> <li class="alt"><span>} </span></li> <li><span>} </span></li> <li class="alt"><span>} </span></li> </ol>
1