分享经常用到的21个PHP函数代码段(上)(1)_PHP教程

WBOY
Lepaskan: 2016-07-20 10:57:23
asal
983 orang telah melayarinya

下面介绍的是,在PHP开发中,经常用到的21个函数代码段,当我们用到的时候,就可以直接用了。

1. PHP可阅读随机字符串

此代码将创建一个可阅读的字符串,使其更接近词典中的单词,实用且具有密码验证功能。

<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 class="vars">$max</span><span>; </span><span class="vars">$i</span><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>
Salin selepas log masuk

2. PHP生成一个随机字符串

如果不需要可阅读的字符串,使用此函数替代,即可创建一个随机字符串,作为用户的随机密码等。

<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 class="vars">$l</span><span>; </span><span class="vars">$i</span><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>
Salin selepas log masuk

3. PHP编码电子邮件地址

使用此代码,可以将任何电子邮件地址编码为 html 字符实体,以防止被垃圾邮件程序收集。

<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></a></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> = ‘’;  </span>
</li>
<li>
<span class="vars">$encoded</span><span> = ‘<script>&rsquo;;  </script></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> .= ‘’;  </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>
Salin selepas log masuk

4. PHP验证邮件地址

电子邮件验证也许是中最常用的网页表单验证,此代码除了验证电子邮件地址,也可以选择检查邮件域所属 DNS 中的 MX 记录,使邮件验证功能更加强大。

<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>
Salin selepas log masuk

5. PHP列出目录内容

<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><span class="vars">$dir</span><span>.</span><span class="vars">$file</span><span>.’”>’.</span><span class="vars">$file</span><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>
Salin selepas log masuk

1

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445762.htmlTechArticle下面介绍的是,在 PHP 开发中,经常用到的21个函数代码段,当我们用到的时候,就可以直接用了。 1. PHP可阅读随机字符串 此代码将创建一...
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan