Home > Backend Development > PHP7 > How to use PHP7.0 for regular expression matching?

How to use PHP7.0 for regular expression matching?

WBOY
Release: 2023-05-31 08:10:35
Original
1403 people have browsed it

With the development of Internet technology, PHP, as a popular programming language, has been widely used in Web development. Regular expressions are also one of the commonly used tools in web development for matching strings and text. PHP version 7.0 and above provide powerful support for regular expressions. This article explains how to use PHP 7.0 for regular expression matching.

  1. Basic syntax of regular expressions

Regular expression is a string matching pattern, consisting of some characters and special symbols. In PHP, you can use the preg_match() function for regular expression matching. The following is some basic syntax of regular expressions:

  • Metacharacters

Metacharacters are special characters in regular expressions, used to represent some special matching rules. For example, the period (.) means matching any character, the asterisk (*) means matching zero or more characters, the plus sign ( ) means matching one or more characters, and the question mark (?) means matching zero or one character.

  • Character set

The character set is enclosed in square brackets, which means matching any character in a character set. For example, the character set [abc] means match any of the characters a, b, or c.

  • Escape characters

Some special characters need to be escaped before they can be used. For example, backslash () represents an escape character, and a left bracket (() represents the start of a capture group. , the right bracket ()) indicates the end of the capture group.

  1. Usage of preg_match() function

In PHP, you can use the preg_match() function to match regular expressions. The basic syntax of this function is as follows:

preg_match(pattern, subject, matches, flags, offset);
Copy after login

Parameter description:

  • pattern: regular expression pattern;
  • subject: target string;
  • matches (optional): array in which matching results are stored;
  • flags (optional): matching flags, optional values ​​include: i, m, s and U;
  • offset (optional) (optional): Search the offset of the starting position.

The following is an example of regular expression matching on a string:

$str = "Hello, world!";
if (preg_match("/Hello/", $str) === 1) {
    echo "匹配成功!";
} else {
    echo "匹配失败!";
}
Copy after login

The above code will output: Match successful!

  1. Commonly used regular expression patterns
  • Match numbers
preg_match("/^d+$/", $str);
Copy after login
  • Match dates
preg_match("/^d{4}-d{2}-d{2}$/", $str);
Copy after login
  • Match email address
preg_match("/^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$/", $str);
Copy after login
  • Match URL
preg_match("/^(http|https)://[a-z0-9]+([-.]{1}[a-z0-9]+)*.[a-z]{2,5}(([0-9]{1,5})?/.*)?$/", $str);
Copy after login
  • Match mobile phone number
preg_match("/^1[3456789]d{9}$/", $str);
Copy after login
  1. Usage of preg_match_all() function

The preg_match_all() function is similar to the preg_match() function and is also used for regular expression matching. The difference is that it matches all occurrences in the target string and stores all matching results in an array instead of returning the first matching result. The basic

syntax of the function is as follows:

preg_match_all(pattern, subject, matches, flags, offset);
Copy after login

The parameter description is the same as the preg_match() function.

The following is an example:

$str = "https://www.example.com https://www.google.com https://www.facebook.com";
preg_match_all("/https://www.w+.com/", $str, $matches);
print_r($matches[0]);
Copy after login

The above code will output:

Array
(
    [0] => https://www.example.com
    [1] => https://www.google.com
    [2] => https://www.facebook.com
)
Copy after login
  1. preg_replace() usage

preg_replace () function is used to replace regular expressions in the target string. The basic syntax of the function is as follows:

preg_replace(pattern, replacement, subject, limit, count);
Copy after login

Parameter description:

  • pattern: regular expression pattern;
  • replacement: replacement string or callback function;
  • subject: target string;
  • limit (optional): the maximum number of substitutions;
  • count (optional): a variable used to store the number of substitutions.

The following is an example:

$str = "Hello, world!";
echo preg_replace("/Hello/", "Hi", $str);
Copy after login

The above code will output: Hi, world!

In short, regular expressions are a very powerful tool. Very useful when doing string matching and replacement. PHP 7.0 provides powerful regular expression support, making using regular expressions in PHP very easy. We only need to learn the basic syntax and common patterns of regular expressions to easily match and replace strings.

The above is the detailed content of How to use PHP7.0 for regular expression matching?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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