10個(gè)適用的PHP正則表達(dá)式
Jun 13, 2016 pm 12:17 PM
10個(gè)實(shí)用的PHP正則表達(dá)式
正則表達(dá)式是程序開(kāi)發(fā)中一個(gè)重要的元素,它提供用來(lái)描述或匹配文本的字符串,如特定的字符、詞或算式等。但在某些情況下,用正則表達(dá)式去驗(yàn)證一個(gè)字符串比較復(fù)雜和費(fèi)時(shí)。本文為你介紹10種常見(jiàn)的實(shí)用PHP正則表達(dá)式的寫(xiě)法,希望對(duì)你的工作有所幫助。
1. 驗(yàn)證E-mail地址
這是一個(gè)用于驗(yàn)證電子郵件的正則表達(dá)式。但它并不是高效、完美的解決方案。在此不推薦使用。
1 2
3
4
5
6
|
$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" ; } |
為了更加有效驗(yàn)證電子郵件地址,推薦使用filer_var。
1 2
3
4
5
|
if (filter_var( [email?protected]' , FILTER_VALIDATE_EMAIL)) { ???? echo "Your email is ok." ; } else { ???? echo "Wrong email address format." ; } |
2. 驗(yàn)證用戶名
這是一個(gè)用于驗(yàn)證用戶名的實(shí)例,其中包括字母、數(shù)字(A-Z,a-z,0-9)、下劃線以及最低5個(gè)字符,最大20個(gè)字符。同時(shí),也可以根據(jù)需要,對(duì)最小值和最大值做合理的修改。
1 2
3
4
5
6
|
$username = "user_name12" ; if (preg_match( '/^[a-z\d_]{5,20}$/i' , $username )) { ???? echo "Your username is ok." ; } else { ???? echo "Wrong username format." ; } |
3. 驗(yàn)證電話號(hào)碼
這是一個(gè)驗(yàn)證美國(guó)電話號(hào)碼的實(shí)例。
1 2
3
4
5
6
|
$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. 驗(yàn)證IP地址
這是一個(gè)用來(lái)驗(yàn)證IPv4地址的實(shí)例。
1 2
3
4
5
6
|
$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. 驗(yàn)證郵政編碼
這是一個(gè)用來(lái)驗(yàn)證郵政編碼的實(shí)例。
1 2
3
4
5
6
|
$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. 驗(yàn)證SSN(社會(huì)保險(xiǎn)號(hào))
這是一個(gè)驗(yàn)證美國(guó)SSN的實(shí)例。
1 2
3
4
5
6
|
$ssn = "333-23-2329" ; if (preg_match( '/^[\d]{3}-[\d]{2}-[\d]{4}$/' , $ssn )) { ???? echo "Your SSN is ok." ; } else { ???? echo "Wrong SSN." ; } |
7. 驗(yàn)證信用卡號(hào)
1 2
3
4
5
6
|
$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. 驗(yàn)證域名
1 2
3
4
5
6
|
$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中提取域名
1 2
3
4
|
$url = "http://ansoncheung.tk/articles" ; ? preg_match( '@^(?:http://)?([^/]+)@i' , $url , $matches ); ? $host = $matches [1]; echo $host ;
|
10. 將文中關(guān)鍵詞高亮顯示
1 2
3
|
$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" , '<span style="background:#5fc9f6">\1</span>' , $text ); echo $text ;
|

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

??? ??











??? ??? "??? ???? PIN ??? ?????"?? ???? ?????. ?? ?? ??? ??? ? ?? ?? ?? ?? ??? ???? ????? PIN ?? ??? ??? ?? ?????. ??? ?? ??? ???? Windows? ???? ?? ?????? ?? ???? ???? ????. ?? ?? ?? ????. ??? ??? ???? ???? ?? ??? ???? ?????. ???? Windows 11?? PIN? ????? ???? ??? ?????? ??? ??? ??? ???? ?? ? ???? ?? ???? ?? ?? ?? ?????. ??? ????? ???? ??? ? ? ????! ?? ?? ??? ?? ?? ???? ??? ????? ?? ?? ??? ??? ? ????. ?? ??

Windows 11? ???? ??? ???? ??? ??????. ???? ?????? ?? ? ???? ?? ??? ?? ??? ????? ??? ? ????. ? ?????? Windows ?? ???? ??? ???? ???? ??? ??? ? ??? ?? ??? ??? ?????. ? ??? ??? ???? ??? ?????? +? ?? ?? ?? ???. Windows?? ???? ???? ?? ??? ?????. ?? ?? ? ??? ?? ? 11" Width="643" Height="500" > ?? ??? ? ? ???? ?? ?? ?? ??? ?? ?? ?? ???? ?????. ?? ?? ? ?? ???? ?? ??? ????? ?? ??? ?? ???? ?? ??? ????? ?? ??? ?? ???? ?? ??? ???.

????? Windows 11? ?? ??? ??? ??? ???/?? ??? ?? ????. ??? ??? ???? ??? ? ????. ? ?????? ?? ???? ???? ??? ????? ????? ????? ??? ? ?? ??? ?? ??? ??? ?????. ?? ?? ??? ?? ?? ??? ??? ??? ? ????? ?, ?? ?? ???? ?? ?? ?? ??? ??? ????? ????? ???? ???? ??? ?? ?? ??? ??? ??? ? ????. ??? ??? ????? ?? ???? ?????. Windows 11?? ?? ??? ??? ???? ??? ?????? 1. ?? ?? ???? +? ?? ?? ?? ???. Windows"?? ??"?? ??? ??

?? ??? ???? ???? ?? ??? ??? ???? ??? ???? ? ?? ????. ? ?? ?? ??? ?? ???? ????? ???? ??? ??? ?? ? ? ??? ?? ????. ? ?? ??? ? ?? ??? ???? ????? ????. ??? ??? ???? ?? ? ?? ??? ?? ??? ?????? ??? ????????. ??? ???? ??? ?? ??? ? ?? ?? ??? ??? ?? ???? ? ????. Windows 11?? ?? ??? ??? ?? ??? ????? ??? ?????? 1. ?? ?? ???? ?? ??? ??? ?????. Windows??? ???? ???? ??? ?????. ?? ??? ??? ?????. ?? ??? ???? ?? ???? ??? ?????. "?? ??"? ?????.

Windows Installer ???? "OOBELANGUAGE" ?? ?? "??? ??????."? ?????? ??? ??? ?? Windows ??? ???? ??? ????. OOBE? ?? ?? ??? ??? ?????. ?? ????? ? ? ??? ?? OOBE ?? ??? ??? ?????. ??? ??? ????. OOBE ?? ???? ?????? ???? ? ??? ??? ? ????. ?? ?? – 1. OOBE ? ??? ?? “?? ??” ??? ?????. ??? ? ??? ?? ?? ????? ?????. 2. ?? ??? ???? ???? ?? ?????. ???? ?? ??? ? OOBE? ????? ???. 3. ????? ??? ??? ????. ???? ???? OOBE? ?? ??? ?????.

Windows 11? ????? ?? ??? ???? ?? ??? ?? ?? ???? ??? ????. ? ???? ???? ??? ??, ?? ???? ???? ??? ????. ??? ??? ?? ??? ????? ??? ??? ?????. ??? ?? ?? ???? ???? ??? ?? ??? ?? ? ???? ???? ? ???? ??? ??? ??? ????? ?? ??? ???? ??? ??? ???. Custom Zoom? ??: ??? ???? ?? ??? ????? ??? ?????. ? ?? ???? ? ?? ?? ? ? ??? ?????. ?? ??? ? ?? ?????? ???? ??? ?? ?? ???? ??? ? ????. ??? ????? ??? ????? ? ??? ? ? ????. ?? ?? ??? ??? ? ????? ??? ? ????. ??? 11? ???? ??

?? ??? ?? ??? ??? ??? ? ???? ????, ?? ??? ??? ? ? ?? ?????. ?? ??? ???, ???? ???, ???? ?? ????? ?? ? ??? ???. ??? ??? ?? ?? ??? ??? ? ???, ?? ??? UI ??? ??? Windows 11??? ?? ?????. ??? ???? ? ??? ?? ?? Windows 11?? ??? ???? ?? ??? ??? ????. Windows 11?? ??? ???? ?? [10?? ??] ?? ??? ???? ?? ??? ???? Windows 11?? ??? ??? ? ????. ???? ?? ???? ???? ???? ???? ???? ?????. ????. ?? 1: ?? ?? ?? ?? ??? ???? ? ????.

Windows? ?? ?? ?????? ??? ? ?? ?? 0xc004f069? ??? ?? ???? ???? ??? ????. ??? ????? ????? ?????? Windows Server? ???? ?? ?? ????? ? ??? ??? ? ????. ??? ?? ??? ???? ??? ???? ??? ?? ??? ?? ?? ???? ???? ??? ??????. ?? ?? - ?? ???? ??? ?? ????. ?? ?? ???? ?? ??????. Windows ?? ?? ????? ???? ?? ?????. ?? 1 – ????? ??? cmd ????? Windows Server Edition ???? ??????. 1?? – Windows Server ?? ?? ?? ???? ?? W ??? ???? ???.
