以下に、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><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 はランダムな文字列を生成します
読み取り可能な文字列が必要ない場合は、代わりにこの関数を使用して、ユーザーのランダムなパスワードなどとしてランダムな文字列を作成します。
<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 でエンコードされたメール アドレス
このコードを使用すると、メール アドレスを 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 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 検証電子メール アドレス
電子メール検証は、電子メール アドレスの検証に加えて、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>
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 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