PHP regular expression method to verify specific year

PHPz
Release: 2023-06-24 17:12:01
Original
1186 people have browsed it

PHP regular expression method to verify a specific year

In the process of website development, it is often necessary to verify whether a specific year meets the format requirements. At this time, you can use PHP regular expressions to verify.

First, we need to understand the format of the year. Common year formats are as follows:

  1. 1970
  2. 2000
  3. 2021
  4. 9999

According to For these year formats, we can write corresponding regular expressions.

First, for the four-digit year, we can use the following regular expression to verify:

$pattern = '/^(19|20)d{2}$/';
Copy after login

The above regular expression uses the regular expression metacharacters "^" and "$ " to limit the beginning and end of the verification string; "|" means or; "d" means a number, and "d{2}" means two numbers, that is, matching the last two digits of the year.

In this way, we can use the preg_match() function to verify whether a string meets the format requirements of a specific year:

if(preg_match($pattern, $year)) { // 符合格式要求 } else { // 不符合格式要求 }
Copy after login

Next, for two-digit years, we can also use Regular expression for verification:

$pattern = '/^(19|20)?d{2}$/';
Copy after login

In this regular expression, we use "?" to indicate that the previous item is optional. In this way, we can match the two-digit number "99" in 1999, or the four-digit number "1999".

Finally, if the specific year we need to verify is within a certain range, such as between 2000 and 2020, we can use the following regular expression:

$pattern = '/^20(0[0-9]|1[0-9])$/';
Copy after login

In this regular expression In the expression, we use "[]" to match any one of the characters; "|" means or. "0[0-9]" means starting with 0, followed by a digit, that is, "00"-"09"; "1[0-9]" means starting with 1, followed by a digit, that is, "10"- "19". This way we can match the range 2000-2019.

Through the above examples, we can see that the PHP regular expression method of validating a specific year is very flexible and practical. In daily development, as long as you master the basic syntax of regular expressions and write different pattern matching rules based on actual needs, you can easily cope with various verification requirements.

The above is the detailed content of PHP regular expression method to verify specific year. 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
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!