網站開發常用的10個PHPRegex(推薦)

來源:互聯網
上載者:User

http://www.php.cn/wiki/588.html" target="_blank" style="line-height: 1.76em;">Regex是程式開發中一個重要的元素,它提供用來描述或匹配文本的字串,如特定的字元、詞或算式等。但在某些情況下,用Regex去驗證一個字串比較複雜和費時。本文為你介紹10種常見的實用PHPRegex的寫法,希望對你的工作有所協助。

1. 驗證E-mail地址

這是一個用於驗證電子郵件的Regex。但它並不是高效、完美的解決方案。在此不推薦使用。

$email = "[email protected]";if (preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',$email)) {echo "Your email is ok.";} else {echo "Wrong email address format";}

為了更加有效驗證電子郵件地址,推薦使用filer_var。

if (filter_var([email protected]', FILTER_VALIDATE_EMAIL)) {echo "Your email is ok.";} else {echo "Wrong email address format.";}

2. 驗證使用者名稱

這是一個用於驗證使用者名稱的執行個體,其中包括字母、數字(A-Z,a-z,0-9)、底線以及最低5個字元,最大20個字元。同時,也可以根據需要,對最小值和最大值做合理的修改。

$username = "user_name12";if (preg_match('/^[a-z\d_]{5,20}$/i', $username)) {echo "Your username is ok.";} else {echo "Wrong username format.";}

3. 驗證電話號碼

這是一個驗證美國電話號碼的執行個體。

$phone = "(021)423-2323";if (preg_match('/\(?\d{3}\)?[-\s.]?\d{3}[-\s.]\d{4}/x', $phone)) {echo "Your phone number is ok.";} else {echo "Wrong phone number.";}

4. 驗證IP地址

這是一個用來驗證IPv4地址的執行個體。

$IP = "198.168.1.78";if (preg_match('/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/',$IP)) {echo "Your IP address is ok.";} else {echo "Wrong IP address.";}

5. 驗證郵遞區號

這是一個用來驗證郵遞區號的執行個體。

$zipcode = "12345-5434";if (preg_match("/^([0-9]{5})(-[0-9]{4})?$/i",$zipcode)) {echo "Your Zip code is ok.";} else {echo "Wrong Zip code.";}

6. 驗證SSN(社會保險號)

這是一個驗證美國SSN的執行個體。

$ssn = "333-23-2329";if (preg_match('/^[\d]{3}-[\d]{2}-[\d]{4}$/',$ssn)) {echo "Your SSN is ok.";} else {echo "Wrong SSN.";}

7. 驗證信用卡號

$cc = "378282246310005";if (preg_match('/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$/', $cc)) {echo "Your credit card number is ok.";} else {echo "Wrong credit card number.";}

8. 驗證網域名稱

$url = "http://ansoncheung.tk/";if (preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $url)) {echo "Your url is ok.";} else {echo "Wrong url.";}

9. 從特定URL中提取網域名稱

4$url = "http://ansoncheung.tk/articles";preg_match('@^(?:http://)?([^/]+)@i', $url, $matches);$host = $matches[1];echo $host;

10. 將文中關鍵詞高亮顯示

$text = "Sample sentence from AnsonCheung.tk, regular expression has become popular in web programming. Now we learn regex. According to wikipedia, Regular expressions (abbreviated as regex or regexp, with plural forms regexes, regexps, or regexen) are written in a formal language that can be interpreted by a regular expression processor";$text = preg_replace("/\b(regex)\b/i", '\1', $text);echo $text;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.