10 useful php Regular expressions, PHP regular expression _php tutorial

Source: Internet
Author: User
Tags php regular expression

10 useful PHP Regular expressions, useful PHP regular expressions


A regular expression is an important element in the development of a program that provides a string that describes or matches text, such as a specific character, word, or calculation. But in some cases, using regular expressions to verify that a string is more complex and time-consuming. This article gives you a description of 10 common PHP regular expressions that I hope will help you with your work.

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.

123456 $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 " }

For more effective verification of e-mail addresses, we recommend using Filer_var.

12345 if(filter_var('test+email@ansoncheung', 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.

123456 $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 the phone number

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

123456 $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.

123456 $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.

123456 $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.

123456 < Code class= "PHP variable" > $ssn = "333-23-2329" if (Preg_match ( '/^[\d]{3}-[\d]{2}-[\d]{4}$/' $ssn

7. Verify the credit card number

123456 $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. Verify the domain name

123456 $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

1234 $url = "/http" Ansoncheung.tk/articles " preg_match ( ' @^ (?: http ://)? ([^/]+) @i ' $url $matches $host = $matches [1]; echo $host

10. Highlight the keywords in the text

123 $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", ', $text);echo$text;

http://www.bkjia.com/PHPjc/973026.html www.bkjia.com true http://www.bkjia.com/PHPjc/973026.html techarticle 10 useful php Regular expressions, a useful php regular expression Regular expression is an important element in program development, it provides a string to describe or match text, such as a specific word ...

  • Related Article

    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.