10個實用的PHPRegex匯總,phpRegex匯總_PHP教程

來源:互聯網
上載者:User

10個實用的PHPRegex匯總,phpRegex匯總


本文執行個體講述了10個實用的PHPRegex匯總,分享給大家供大家參考。具體如下:

Regex是程式開發中一個重要的元素,它提供用來描述或匹配文本的字串,如特定的字元、詞或算式等。但在某些情況下,用Regex去驗證一個字串比較複雜和費時。本文為你介紹10種常見的實用PHPRegex的寫法,希望對你的工作有所協助。

1. 驗證E-mail地址

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

複製代碼 代碼如下:$email = "test@ansoncheung.tk";
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('test+email@ansoncheung', 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中提取網域名稱

複製代碼 代碼如下:$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;

希望本文所述對大家的PHP程式設計有所協助。


php 怎使用Regex從一段內容裡提取出一段連續的10位元字

[^\d](?\d{10})([^\d]|$)
 

PHP 怎在一個Regex裡使用變數?

int preg_match ( string $pattern , string $subject [, array $matches [, int $flags ]] )
string $pattern 是一個字串,所以可以先計算出這個串存入一個變數裡
$star='a';
$stop='c';
$info='a1b2c3';
$pattern='/'.$star.'(.+?)'.$stop.'/';
preg_match($pattern,$info,$result);
print_r($result[1]);
 

http://www.bkjia.com/PHPjc/898284.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/898284.htmlTechArticle10個實用的PHPRegex匯總,phpRegex匯總 本文執行個體講述了10個實用的PHPRegex匯總,分享給大家供大家參考。具體如下: 正則...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.