php郵箱地址正則表達式驗證
我們最經(jīng)常遇到的驗證,就是電子郵件地址驗證。網(wǎng)站上常見。各種網(wǎng)頁腳本也都常用“正則表達式”(regular expression)對我們輸入的電子郵件地址進行驗證,判斷是否合法。有的還能分解出用戶名和域名?,F(xiàn)在用PHP語言實現(xiàn)一下電子郵件地址驗證程序,用的是PHP正則表達式庫。
源代碼如下:
<?php header ( "Content-Type: text/html; charset=UTF-8" ); $reply = ""; if ( isset($_POST["email_address"]) ) { $email_address = $_POST["email_address"]; $pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i"; if ( preg_match( $pattern, $email_address ) ) { $reply = "您輸入的電子郵件地址合法<br /><br />\n"; $user_name = preg_replace( $pattern ,"$1", $email_address ); $domain_name = preg_replace( $pattern ,"$2", $email_address ); $reply .= "用戶名:".$user_name."<br />\n"; $reply .= "域名:".$domain_name."<br />\n\n"; } else { $reply = "您輸入的電子郵件地址不合法"; } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="zh" xml:lang="zh"> <head> <title>電子郵件地址驗證程序</title> </head> <body style="text-align: center;"> <h1>電子郵件地址驗證程序</h1> <form action="#" method="post"> 請輸入電子郵件地址:<input name="email_address" type="text" style="width: 300px;" /><br /> <input type="submit" value="驗證電子郵件地址" /> </form> <?php echo $reply; ?> </body> </html>
以上就是為大家分享的php郵箱地址正則表達式驗證,希望對大家的學(xué)習(xí)有所幫助。
相關(guān)文章
php 靜態(tài)屬性和靜態(tài)方法區(qū)別詳解
這篇文章主要介紹了php 靜態(tài)屬性和靜態(tài)方法區(qū)別詳解,需要的朋友可以參考下2017-04-04fetchAll()與mysql_fetch_array()的區(qū)別詳解
本篇文章是對fetchAll()與mysql_fetch_array()的區(qū)別進行了詳細的分析介紹,需要的朋友參考下2013-06-06PHP字符轉(zhuǎn)義相關(guān)函數(shù)小結(jié)(php下的轉(zhuǎn)義字符串)
PHP字符轉(zhuǎn)義相關(guān)函數(shù)小結(jié),有時候為了安全起見,我們需要對用戶輸入的字符串進行轉(zhuǎn)義2007-04-04