学ぶ PHP 検証フォームの具体的なコードは次のとおりです:
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>class class_post </SPAN><LI class=alt><SPAN>{ </SPAN><LI class=""><SPAN> //验证是否为指定长度的字母/数字组合 </SPAN><LI class=alt><SPAN> function fun_text1($num1,$num2,$str) </SPAN><LI class=""><SPAN> { </SPAN><LI class=alt><SPAN> Return (preg_match("/^[a-zA-Z0-9]{".$num1.",".$num2."}$/",$str))?true:false; </SPAN><LI class=""><SPAN> } </SPAN><LI class=alt><SPAN> //验证是否为指定长度数字 </SPAN><LI class=""><SPAN> function fun_text2($num1,$num2,$str) </SPAN><LI class=alt><SPAN> { </SPAN><LI class=""><SPAN> return (preg_match("/^[0-9]{".$num1.",".$num2."}$/i",$str))?true:false; </SPAN><LI class=alt><SPAN> } </SPAN><LI class=""><SPAN> //验证是否为指定长度汉字 </SPAN><LI class=alt><SPAN> function fun_font($num1,$num2,$str) </SPAN><LI class=""><SPAN> { </SPAN><LI class=alt><SPAN> // preg_match("/^[xa0-xff]{1,4}$/", $string); </SPAN><LI class=""><SPAN> return (preg_match("/^([x81-xfe][x40-xfe]){".$num1.",".$num2."}$/",$str))?true:false; </SPAN><LI class=alt><SPAN> } </SPAN><LI class=""><SPAN> //验证身份证号码 </SPAN><LI class=alt><SPAN> function fun_status($str) </SPAN><LI class=""><SPAN> { </SPAN><LI class=alt><SPAN> return (preg_match('/(^([d]{15}|[d]{18}|[d]{17}x)$)/',$str))?true:false; </SPAN><LI class=""><SPAN> } </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN> //验证邮件地址 </SPAN><LI class=alt><SPAN> function fun_email($str){ </SPAN><LI class=""><SPAN> return (preg_match('/^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,4}$/',$str))?true:false; </SPAN><LI class=alt><SPAN> } </SPAN><LI class=""><SPAN> //验证电话号码 </SPAN><LI class=alt><SPAN> function fun_phone($str) </SPAN><LI class=""><SPAN> { </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN> return (preg_match("/^(((d{3}))|(d{3}-))?((0d{2,3})|0d{2,3}-)?[1-9]d{6,7}$/",$str))?true:false; </SPAN><LI class=alt><SPAN> } </SPAN><LI class=""><SPAN> //验证邮编 </SPAN><LI class=alt><SPAN> function fun_zip($str) </SPAN><LI class=""><SPAN> { </SPAN><LI class=alt><SPAN> return (preg_match("/^[1-9]d{5}$/",$str))?true:false; </SPAN><LI class=""><SPAN> } </SPAN><LI class=alt><SPAN> //验证url地址 </SPAN><LI class=""><SPAN> function fun_url($str) </SPAN><LI class=alt><SPAN> { </SPAN><LI class=""><SPAN> return (preg_match("/^http://[A-Za-z0-9]+.[A-Za-z0-9]+[/=?%-&_~`@[]':+!]*([^</SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag>></span></font></strong><span>""])*$/",$str))?true:false; </span></span></li> <li class="alt"><span> } </span></li> <li class=""><span> </span></li> <li class="alt"><span> // 数据入库 转义 特殊字符 传入值可为字符串 或 一维数组 </span></li> <li class=""><span> function data_join(&$data) </span></li> <li class="alt"><span> { </span></li> <li class=""><span> if(get_magic_quotes_gpc() == false) </span></li> <li class="alt"><span> { </span></li> <li class=""><span> if (is_array($data)) </span></li> <li class="alt"><span> { </span></li> <li class=""> <span> foreach ($data as $</span><span class="attribute"><font color="#ff0000">k</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> $v) </span> </li> <li class="alt"><span> { </span></li> <li class=""><span> $data[$k] = addslashes($v); </span></li> <li class="alt"><span> } </span></li> <li class=""><span> } </span></li> <li class="alt"><span> else </span></li> <li class=""><span> { </span></li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">data</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">addslashes</font></span><span>($data); </span> </li> <li class=""><span> } </span></li> <li class="alt"><span> } </span></li> <li class=""><span> Return $data; return (preg_match("/^(((d{3}))|(d{3}-))?((0d{2,3})|0d{2,3}-)?[1-9]d{6,7}$/",$str))?true:false; </span></li> <li class="alt"><span> } </span></li> <li class=""><span> </span></li> <li class="alt"><span> // 数据出库 还原 特殊字符 传入值可为字符串 或 一/二维数组 </span></li> <li class=""><span> function data_revert(&$data) </span></li> <li class="alt"><span> { </span></li> <li class=""><span> if (is_array($data)) </span></li> <li class="alt"><span> { </span></li> <li class=""> <span> foreach ($data as $</span><span class="attribute"><font color="#ff0000">k1</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> $v1) </span> </li> <li class="alt"><span> { </span></li> <li class=""><span> if (is_array($v1)) </span></li> <li class="alt"><span> { </span></li> <li class=""> <span> foreach ($v1 as $</span><span class="attribute"><font color="#ff0000">k2</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> $v2) </span> </li> <li class="alt"><span> { </span></li> <li class=""><span> $data[$k1][$k2] = stripslashes($v2); </span></li> <li class="alt"><span> } </span></li> <li class=""><span> } </span></li> <li class="alt"><span> else </span></li> <li class=""><span> { </span></li> <li class="alt"><span> $data[$k1] = stripslashes($v1); </span></li> <li class=""><span> } </span></li> <li class="alt"><span> } </span></li> <li class=""><span> } </span></li> <li class="alt"><span> else </span></li> <li class=""><span> { </span></li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">data</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">stripslashes</font></span><span>($data); </span> </li> <li class=""><span> } </span></li> <li class="alt"><span> Return $data; </span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span> // 数据显示 还原 数据格式 主要用于内容输出 传入值可为字符串 或 一/二维数组 </span></li> <li class="alt"><span> // 执行此方法前应先data_revert(),表单内容无须此还原 </span></li> <li class=""><span> function data_show(&$data) </span></li> <li class="alt"><span> { </span></li> <li class=""><span> </span></li> <li class="alt"><span> if (is_array($data)) </span></li> <li class=""><span> { </span></li> <li class="alt"> <span> foreach ($data as $</span><span class="attribute"><font color="#ff0000">k1</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> $v1) </span> </li> <li class=""><span> { </span></li> <li class="alt"><span> if (is_array($v1)) </span></li> <li class=""><span> { </span></li> <li class="alt"> <span> foreach ($v1 as $</span><span class="attribute"><font color="#ff0000">k2</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> $v2) </span> </li> <li class=""><span> { </span></li> <li class="alt"><span> $data[$k1][$k2]=nl2br(htmlspecialchars($data[$k1][$k2])); </span></li> <li class=""><span> $data[$k1][$k2]=str_replace(" "," ",$data[$k1][$k2]); </span></li> <li class="alt"> <span> $data[$k1][$k2]=str_replace(" ","</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span></font></strong><span> ",$data[$k1][$k2]); </span> </li> <li class=""><span> } </span></li> <li class="alt"><span> } </span></li> <li class=""><span> else </span></li> <li class="alt"><span> { </span></li> <li class=""><span> $data[$k1]=nl2br(htmlspecialchars($data[$k1])); </span></li> <li class="alt"><span> $data[$k1]=str_replace(" "," ",$data[$k1]); </span></li> <li class=""> <span> $data[$k1]=str_replace(" ","</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span></font></strong><span> ",$data[$k1]); </span> </li> <li class="alt"><span> } </span></li> <li class=""><span> } </span></li> <li class="alt"><span> } </span></li> <li class=""><span> else </span></li> <li class="alt"><span> { </span></li> <li class=""> <span> $</span><span class="attribute"><font color="#ff0000">data</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">nl2br</font></span><span>(htmlspecialchars($data)); </span> </li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">data</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">str_replace</font></span><span>(" "," ",$data); </span> </li> <li class=""> <span> $</span><span class="attribute"><font color="#ff0000">data</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">str_replace</font></span><span>(" ","</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span></font></strong><span> ",$data); </span> </li> <li class="alt"><span> } </span></li> <li class=""><span> Return $data; </span></li> <li class="alt"><span> } </span></li> <li class=""><span>} </span></li> <li class="alt"><span>if (is_array($data)) </span></li> <li class=""><span>{ </span></li> <li class="alt"> <span> foreach ($data as $</span><span class="attribute"><font color="#ff0000">k1</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> $v1) </span> </li> <li class=""><span> { </span></li> <li class="alt"><span> if (is_array($v1)) </span></li> <li class=""><span> { </span></li> <li class="alt"> <span> foreach ($v1 as $</span><span class="attribute"><font color="#ff0000">k2</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> $v2) </span> </li> <li class=""><span> { </span></li> <li class="alt"><span> $data[$k1][$k2]=nl2br(htmlspecialchars($data[$k1][$k2])); </span></li> <li class=""><span> $data[$k1][$k2]=str_replace(" "," ",$data[$k1][$k2]); </span></li> <li class="alt"> <span> $data[$k1][$k2]=str_replace(" ","</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span></font></strong><span> ",$data[$k1][$k2]); </span> </li> <li class=""><span> } </span></li> <li class="alt"><span> } </span></li> <li class=""><span> else </span></li> <li class="alt"><span> { </span></li> <li class=""><span> $data[$k1]=nl2br(htmlspecialchars($data[$k1])); </span></li> <li class="alt"><span> $data[$k1]=str_replace(" "," ",$data[$k1]); </span></li> <li class=""> <span> $data[$k1]=str_replace(" ","</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span></font></strong><span> ",$data[$k1]); </span> </li> <li class="alt"><span> } </span></li> <li class=""><span> } </span></li> <li class="alt"><span>} </span></li> <li class=""><span>else </span></li> <li class="alt"><span>{ </span></li> <li class=""> <span> $</span><span class="attribute"><font color="#ff0000">data</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">nl2br</font></span><span>(htmlspecialchars($data)); </span> </li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">data</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">str_replace</font></span><span>(" "," ",$data); </span> </li> <li class=""> <span> $</span><span class="attribute"><font color="#ff0000">data</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">str_replace</font></span><span>(" ","</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span></font></strong><span> ",$data); </span> </li> <li class="alt"><span>} </span></li> <li class=""><span>Return $data; </span></li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
上記のコードは関連する実装ですPHP検証フォームのメソッド。