Practical PHP Regular Expressions

Regular expressions are an important element in program development; they provide strings used to describe or match text, such as specific characters, words, or expressions. But in some cases, using a regular expression to validate a string is fairly complicated and time-consuming. This article introduces 10 common and practical ways to write PHP regular expressions, and I hope they are helpful in your work. 1. Validate an E-mail address This is a regular expression used to validate email. But it is not an efficient, perfect solution. Its use is not recommended here.

Php code

1
2
3
4
5
6
$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";
}


To validate email addresses more effectively, using filer_var is recommended.

Php code

1
2
3
4
5
if (filter_var('test+email@ansoncheung', FILTER\_VALIDATE\_EMAIL)) {
    echo "Your email is ok.";
} else {
    echo "Wrong email address format.";
}

2. Validate a username This is an example used to validate usernames, which include letters, digits (A-Z, a-z, 0-9), underscores, and a minimum of 5 characters and a maximum of 20 characters. At the same time, you can make reasonable changes to the minimum and maximum values as needed.

Php code

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. Validate a phone number This is an example that validates US phone numbers.

Php code

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. Validate an IP address This is an example used to validate IPv4 addresses.

Php code

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. Validate a postal code This is an example used to validate postal codes.

Php code

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. Validate an SSN (Social Security Number) This is an example that validates a US SSN.

Php code

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. Validate a credit card number

Php code

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. Validate a domain name

Php code

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. Extract the domain name from a specific URL

Php code

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

echo $host;

10. Highlight keywords in the text

Php code

1
2
3
4
5
$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;

Originally from http://www.iteye.com/news/23231 ITeye黑板报