Home  >  Article  >  Backend Development  >  Share 21 frequently used PHP function code snippets (Part 1) (1)_PHP Tutorial

Share 21 frequently used PHP function code snippets (Part 1) (1)_PHP Tutorial

WBOY
WBOYOriginal
2016-07-20 10:57:23977browse

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 &ndash; 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>= &ldquo;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789&Prime;;  </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>=&rsquo;info@domain.com&rsquo;, </span><span class="vars">$linkText</span><span>=&rsquo;Contact Us&rsquo;, </span></span></li><li class="alt"><span><span class="vars">$attrs</span><span> =&rsquo;</span><span class="keyword">class</span><span>=&rdquo;emailencoder&rdquo;&lsquo; )  </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>(&lsquo;@&rsquo;, &lsquo;&#64;&rsquo;, </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>(&lsquo;.&rsquo;, &lsquo;&#46;&rsquo;, </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>(&lsquo;@&rsquo;, &lsquo;&#64;&rsquo;, </span><span class="vars">$linkText</span><span>);  </span></li><li><span class="vars">$linkText</span><span> = </span><span class="func">str_replace</span><span>(&lsquo;.&rsquo;, &lsquo;&#46;&rsquo;, </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> = &lsquo;<a href=&rdquo;ma&rsquo;;  </span></li><li class="alt"><span class="vars">$part2</span><span> = &lsquo;ilto&#58;&rsquo;;  </span></li><li><span class="vars">$part3</span><span> = &lsquo;&rdquo; &lsquo;. </span><span class="vars">$attrs</span><span> .&rsquo; >’;  </span>
</li>
<li class="alt">
<span class="vars">$part4</span><span> = ‘</a>’;  </span>
</li>
<li>
<span class="vars">$encoded</span><span> = ‘<script type=&rdquo;text/javascript&rdquo;>’;  </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=&rdquo;_blank&rdquo; href=&rdquo;&lsquo;.</span><span class="vars">$dir</span><span>.</span><span class="vars">$file</span><span>.&rsquo;&rdquo;>’.</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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445762.htmlTechArticleIntroduced below are 21 function code segments that are often used in PHP development. When we use , you can use it directly. 1. PHP can read random strings. This code will create a...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn