10 Practical PHP Regular expression Rollup _php tips

Source: Internet
Author: User
Tags php programming php regular expression

This article describes 10 practical PHP regular Expression summary, share for everyone to reference. Specifically as follows:

Regular expressions are an important element in program development, which provides strings for describing or matching text, such as specific characters, words, or expressions. However, in some cases, it is more complicated and time-consuming to validate a string with a regular expression. This article introduces you to the 10 common practical php regular expressions, hoping to help you with your work.

1. Verify e-mail address

This is a regular expression that is used to validate an e-mail message. But it's not an efficient, perfect solution. Use is not recommended here.

Copy Code code as follows:
$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 e-mail addresses more effectively, it is recommended that you use Filer_var.

Copy Code code as follows:
if (Filter_var (' Test+email@ansoncheung ', Filter_validate_email)) {
echo "Your email is OK";
} else {
echo "wrong email address format."
}

2. Verify User Name

This is an instance that validates the username, including letters, numbers (a-z,a-z,0-9), underscores, and a minimum of 5 characters, with a maximum of 20 characters. At the same time, can also be based on the need for the minimum and maximum value to make reasonable changes.

Copy Code code as follows:
$username = "User_name12";
if (Preg_match ('/^[a-z\d_]{5,20}$/i ', $username)) {
echo "Your username is ok";
} else {
echo "wrong username format."
}

3. Verify Telephone number

This is an example of verifying a U.S. phone number.

Copy Code code as follows:
$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 IP Address

This is an instance used to validate the IPV4 address.

Copy Code code as follows:
$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 ZIP Code

This is an instance used to verify the postal code.

Copy Code code as follows:
$zipcode = "12345-5434";
if (Preg_match ([0-9]{5}) (-[0-9]{4})/^, $zipcode)) {
echo "Your Zip code is OK";
} else {
echo "Wrong Zip code."
}

6. Verify SSN (Social Security Number)

This is an example of verifying the American ssn.

Copy Code code as follows:
$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 Credit card number

Copy Code code as follows:
$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 card number is OK";
} else {
echo "wrong card number";
}

8. Verify Domain Name

Copy Code code as follows:
$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

Copy Code code as follows:
$url = "Http://ansoncheung.tk/articles";
Preg_match (' @^: http://)? ( [^/]+) @i ', $url, $matches);
$host = $matches [1];
Echo $host;

10. Highlight the keywords in the text

Copy Code code as follows:
$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 Regexe n) are written in a formal language, that can is interpreted by a regular expression ";
$text = Preg_replace ("/\b (regex) \b/i", "<span style=" background: #5fc9f6 ">\1</span>", $text);
Echo $text;

I hope this article will help you with your PHP programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.