Archive

Posts Tagged ‘php regular expression replace’

10 Common Useful PHP Regular Expression

November 4th, 2011 No comments

Regular expression is a very important element for programmer, it provides a a concise and flexible means for “matching” (specifying and recognizing) strings of text, such as particular characters, words, or patterns of characters. In some suitation, if you need to create your own regular expression for validating a string, it is quite complicated and time consuming. Here, I am going to go through 10 common useful PHP Regular Expression. Next time, you can adopt the following code example if you encounter similar problem in your future project work.

1) Validate E-mail addresses

Here is a basic e-mail validation. However, this is not a efficient and perfect solution of validating email. It is not recommended to use.

$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 address more effectively, it is recommended to use filer_var.

if (filter_var('test+email@ansoncheung', FILTER_VALIDATE_EMAIL)) {
 echo "Your email is ok.";
} else {
 echo "Wrong email address format.";
}
2) Validate usernames

Here is an example to validate a username which includes alpha-numeric (a-z, A-Z, 0-9), underscores, and has minimum 5 character and maximum 20 character. And you can simple change the min. character and max characters as you want.

$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 telephone numbers

Here is an example to validate an US 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) Validate IP addresses

Here is an example to validate an 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) Validate postal codes

Here is an example to validate US Postal 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) Validate SSN

Here is an example to validate US Sosial Security Number.

$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 credit card
$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 domain
$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 domain name from certain URL
$url = "http://ansoncheung.tk/articles";
preg_match('@^(?:http://)?([^/]+)@i', $url, $matches);
$host = $matches[1];

echo $host;
10) Highlight a word in the content
$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;
To Conclude

Some of the regular expression may not be a perfect solution, however it cover most of the case.