Rumah > pembangunan bahagian belakang > tutorial php > 正则表达式的Wed验证应用(40),正则表达式wed_PHP教程

正则表达式的Wed验证应用(40),正则表达式wed_PHP教程

WBOY
Lepaskan: 2016-07-13 09:52:02
asal
964 orang telah melayarinya

正则表达式的Wed验证应用(40),正则表达式wed

电子邮件地址的校验

<?<span>php
</span><span>/*</span><span> 校验邮件地址</span><span>*/</span><span>
function checkMail($email) {
</span><span>//</span><span>用户名,由&ldquo;\w&rdquo;格式字符、&ldquo;-&rdquo;或&ldquo;.&rdquo;组成</span>
$email_name= <span>"</span><span>\w|(\w[-.\w]*\w)</span><span>"</span><span>;
</span><span>//</span><span>域名中的第一段,规则和用户名类似,不包括点号&ldquo;.&rdquo;</span>
$code_at= <span>"</span><span>@</span><span>"</span><span>;
$per_domain</span>= <span>"</span><span>\w|(\w[-\w]*\w)</span><span>"</span><span>;
</span><span>//</span><span>域名中间的部分,至多两段</span>
$mid_domain= <span>"</span><span>(\.</span><span>"</span> .$per_domain. <span>"</span><span>){0,2}</span><span>"</span><span>;
</span><span>//</span><span>域名的最后一段,只能为&ldquo;.com&rdquo;、&ldquo;.org&rdquo;或&ldquo;.net&rdquo;</span>
$end_domain= <span>"</span><span>(\.(com|net|org))</span><span>"</span><span>;
$rs</span>=<span> preg_match(
</span><span>"</span><span>/^{$email_name}@{$per_domain}{$mid_domain}{$end_domain}$/</span><span>"</span><span>,
$email
);
</span><span>return</span> (<span>bool</span><span>)$rs;
}
</span><span>//</span><span>测试,下面均返回成功</span>
var_dump( checkMail(<span>"</span><span>root@localhost</span><span>"</span><span>) );
var_dump( checkMail(</span><span>"</span><span>Frank.Roulan@esun.edu.org</span><span>"</span><span>) );
var_dump( checkMail(</span><span>"</span><span>Tom.024-1234@x-power_1980.mail-address.com</span><span>"</span><span>) );
</span>?>
Salin selepas log masuk

URL地址的校验

<?<span>php
</span><span>/*</span><span> 校验URL地址</span><span>*/</span><span>
function checkDomain($domain)
{
</span><span>return</span> ereg(<span>"</span><span>^(http|ftp)s? ://(www\.)?.+(com|net|org)$</span><span>"</span><span>, $domain);
}
$rs</span>= checkDomain(<span>"</span><span>www.taodoor.com</span><span>"</span>);<span>//</span><span>返回假</span>
$rs= checkDomain(<span>"</span><span>http://www.taodoor.com</span><span>"</span>);<span>//</span><span>返回真</span>
?>
Salin selepas log masuk

电话号码

<?<span>php
</span><span>/*</span><span> 校验电话号码</span><span>*/</span><span>
function checkTelno($tel)
{
</span><span>//</span><span>去掉多余的分隔符</span>
$tel= ereg_replace(<span>"</span><span>[\(\)\. -]</span><span>"</span>, <span>""</span><span>, $tel);
</span><span>//</span><span>仅包含数字,至少应为一个6位的电话号(即没有区号)</span>
<span>if</span>(ereg(<span>"</span><span>^\d+$</span><span>"</span><span>, $tel))
{
</span><span>return</span> <span>true</span><span>;
}</span><span>else</span><span>{
</span><span>return</span> <span>false</span><span>;
}
}
$rs</span>= checkTelno(<span>"</span><span>(086)-0411-12345678</span><span>"</span>);<span>//</span><span>返回真</span>
?>
Salin selepas log masuk

邮政编码的校验

<?<span>php
</span><span>/*</span><span> 校验邮政编码</span><span>*/</span><span>
function checkZipcode($code)
{
</span><span>//</span><span>去掉多余的分隔符</span>
$code = preg_replace(<span>"</span><span>/[\. -]/</span><span>"</span>, <span>""</span><span>, $code);
</span><span>//</span><span>包含一个6位的邮政编码</span>
<span>if</span>(preg_match(<span>"</span><span>/^\d{6}$/</span><span>"</span><span>, $code))
{
</span><span>return</span> <span>true</span><span>;
}</span><span>else</span><span>{
</span><span>return</span> <span>false</span><span>;
}
}
$rs</span>= checkZipCode(<span>"</span><span>123456</span><span>"</span>);<span>//</span><span>返回真</span>
?>
Salin selepas log masuk

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1010331.htmlTechArticle正则表达式的Wed验证应用(40),正则表达式wed 电子邮件地址的校验 ? php /* 校验邮件地址 */ function checkMail($email) { // 用户名,由\w格式字符...
Label berkaitan:
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