What are the common regular expressions in PHP programming?

WBOY
Release: 2023-06-12 12:12:02
Original
809 people have browsed it

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})+$/";
Copy after login

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';
}
Copy after login
  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";
Copy after login

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';
}
Copy after login
  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}$/";
Copy after login

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';
}
Copy after login
  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";
Copy after login

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';
}
Copy after login
  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";
Copy after login

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';
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!