10 most useful PHP regular expressions

Source: Internet
Author: User
Tags php regular expression

What is a PHP regular expression? It is a logical formula for string manipulation, which is to make a "rule string" that is used to express a filter logic for a string by using a predefined set of specific characters and combinations of these specific characters. So what are the common PHP regular expressions? Here is a summary of the 10 most useful PHP regular expressions.

1, in order to more effectively verify the email address, we recommend the use of Filer_var.

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 (az,az,09), 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.

$username = "User_name12";  if (Preg_match ('/^[az\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.

$phone = "(021) 4232323";  if (Preg_match ('/?\d3?[ \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.

$IP = "198.168.1.78";  If   (Preg_match ('/^ ([19]?[ 09]|1[09]{2}|2[04][09]|25[05]).) {3} ([19]? [09]|1[09]{2}|2[04][09]|25[05]) ($/', $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.

$zipcode = "123455434";  if (Preg_match ("/^ ([09]{5}) ([09]{4}) $/i", $zipcode)) {  echo "Your Zip code is OK.";  } else {  echo "wrong Z IP code. ";  }

6. Verify SSN (Social Security Number)

This is an example of verifying a US ssn.

$SSN = "333232329";  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[09]{12} (?: [09]{3})? | 5[15][09]{14}|6011[09]{12}|3 (?: 0 [05]| [68] [09]) [09] {11}|3[47][09]{13}) ($/',   $cc)) {  echo "Your credit card number is OK.";  } else {  echo "wrong credit card n Umber. ";  }

8. Verify the domain name

$url = "http://ansoncheung.tk/";  If   (Preg_match ('/^ (http|https|ftp): \/\/([az09][az09_] (?: \. [AZ09] [az09_]) +):? (\d+) \/?/i ',   $url) {  echo "Your URL is ok.";  } else {  echo "wrong URL.";  }

9. Extracting a domain name from a specific URL

4$url = "Http://ansoncheung.tk/articles";  Preg_match (' @^ (?:/HTTP//)? ( [^/]+) @i ', $url, $matches);  $host = $matches [1];  Echo $host;

10. Highlight the keywords in the text

$text = "Sample sentence from ansoncheung.tk, regular expression had become   popular in web programming. Now we learn regex. According to Wikipedia, Regular   expressions (abbreviated as Regex or regexp, with plural forms regexes, regexps,   o R Regexen) is written in a formal language the can be interpreted by a   regular expression processor ";  $text = Preg_replace ("/\b (regex) \b/i", ' \1 ', $text);  Echo $text;

Because regular expressions are primarily applied to text, they are applied in a variety of text editor scenarios, ranging from the famous editor editplus to large editors such as Microsoft Word, Visual Studio, and can use regular expressions to manipulate text content.

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.