10 common PHP regular expressions for website development (recommended)

Source: Internet
Author: User
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. However, in some cases, it is complicated and time-consuming to use a regular expression to verify a string ....

# Wiki/588.html "target =" _ blank "style =" line-height: 1.76em; "> Regular expressions are an important element in program development, it provides a string used to describe or match text, such as a specific character, word, or formula. However, in some cases, it is complicated and time-consuming to use a regular expression to verify a string. This article introduces you to the writing of 10 common practical PHP regular expressions, hoping to help your work.

1. verify the email address

This is a regular expression used to verify the email. But it is not an efficient and perfect solution. It is not recommended 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";}

Filer_var is recommended for more effective email address verification.

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 used to authenticate the user name, including letters, numbers (A-Z, a-z, 0-9), underscores, and a minimum of 5 characters, a maximum of 20 characters. You can also modify the minimum and maximum values as needed.

$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 US phone number verification.

$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 used to verify 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 used to verify 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 insurance number)

This is an instance that verifies SSN in the United States.

$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 is 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. extract domain names from a specific URL

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

10. highlight keywords in the text

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

The above is the details of 10 common PHP regular expressions (recommended) for website development. For more information, see other related articles in the first PHP community!

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.