1. Verify the e-mail address
This is a regular expression for validating e-mail messages. But it's not an efficient, perfect solution. Not recommended for use here.
$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";}
For more effective verification of e-mail addresses, we recommend using Filer_var.
if (Filter_var (' [email protected] ', Filter_validate_email) {echo "Your email is OK";} else {echo "wrong email address Format. ";}
2. Verify the user name
This is an instance that validates the user name, including letters, numbers (a-z,a-z,0-9), underscores, and a minimum of 5 characters, and a maximum of 20 characters. At the same time, the minimum and maximum values can be modified as needed.
$username = "User_name12", if (Preg_match ('/^[a-z\d_]{5,20}$/i ', $username)) {echo "Your username is ok.";} else {echo "W Rong username format. ";}
3. Verify the phone number
This is an example of verifying a U.S. phone number.
$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. Verify the IP address
This is an instance to validate the IPV4 address.
$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. Verify the ZIP code
This is an instance to validate the ZIP code.
$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. Verify SSN (Social Security Number)
This is an example of verifying a US 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. Verify the credit card number
$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 was OK.";} else {echo "wrong credit card number."}
8. Verify the domain name
$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. Extracting a domain name from a specific URL
$url = "Http://ansoncheung.tk/articles"; Preg_match (' @^ (?:/HTTP//)? ( [^/]+) @i ', $url, $matches); $host = $matches [1];echo $host;
10. Highlight the keywords in the text
$text = "Sample sentence from ansoncheung.tk, regular expression had 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 Regexe N) is written in a formal language the can is interpreted by a regular expression processor "; $text = Preg_replace ("/\b (R Egex) \b/i ", ' <span style=" background: #5fc9f6 ">\1</span>", $text); Echo $text;
No reprint without permission: Origin network» Verify e-mail, user name, social Security number, IP address, etc. 10 useful php regular expressions
[GO] Verify 10 useful PHP regular expressions for e-mail, username, Social Security number, IP address, etc.