Home  >  Article  >  Backend Development  >  What are the common regular expressions in PHP programming?

What are the common regular expressions in PHP programming?

WBOY
WBOYOriginal
2023-06-12 10:26:01801browse

PHP, as a common server-side programming language, often needs to use regular expressions in development. Regular expression is a tool used to match strings. It uses specific grammatical rules to describe the pattern of a string and can better handle string matching, search, replacement and other operations.

The following lists common regular expressions in PHP programming:

  1. Matching email addresses
    In development, it is often necessary to verify whether the email address entered by the user is legal. The following is a Regular expression for matching email addresses:
$regex = "/^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/";

You can use the preg_match() function to verify it. Use:

$email = 'test@example.com';
if (preg_match($regex, $email)) {
    echo 'Valid email address';
} else {
    echo 'Invalid email address';
}
  1. Match URL address
    Same as verifying whether the URL address is legal It is also a common requirement. The following is a regular expression for matching URL addresses:
$regex = "/^(http|https|ftp)://([A-Z0-9][A-Z0-9_-]*(?:.[A-Z0-9][A-Z0-9_-]*)+):?(d+)?/?/i";

You can also use the preg_match() function for verification:

$url = 'http://www.example.com';
if (preg_match($regex, $url)) {
    echo 'Valid URL';
} else {
    echo 'Invalid URL';
}
  1. Matches phone numbers
    It is also a common requirement to verify whether the phone number is legal. The following is a regular expression for matching phone numbers:
$regex = "/^[0-9]{3}-?[0-9]{3}-?[0-9]{4}$/";

You can also use the preg_match() function for verification:

$phone = '555-555-5555';
if (preg_match($regex, $phone)) {
    echo 'Valid phone number';
} else {
    echo 'Invalid phone number';
}
  1. Check whether it contains text
    Sometimes you need to determine whether a string contains a certain keyword. The following is a regular expression that matches a certain keyword:
$regex = "/keyword/i";

Similarly You can use the preg_match() function for verification:

$string = 'This is a text with keyword in it';
if (preg_match($regex, $string)) {
    echo 'Keyword found';
} else {
    echo 'Keyword not found';
}
  1. Matching HTML tags
    When processing HTML text, you need to match information such as tag names and attributes. The following is a matching HTML tag name and attribute Regular expression:
$regex = "/<(w+)([^>]+)*>/i";

You can also use the preg_match() function for verification:

$html = '<div class="example">Hello World</div>';
if (preg_match($regex, $html, $match)) {
    echo 'Tag name: ' . $match[1];
    echo 'Tag attributes: ' . $match[2];
} else {
    echo 'HTML tag not found';
}

Summary: Regular expression is a very important tool in PHP programming and is a common application There are verification email addresses, URL addresses, phone numbers, etc., and can also be used to check whether a certain keyword is included and matching HTML tags, etc. Mastering the basic syntax and common functions of regular expressions can improve program development efficiency and code readability.

The above is the detailed content of What are the common regular expressions in PHP programming?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn