php-regexHow to use PHP regex to match a year?
To use PHP regex to match a year, you can use the following code:
$year = preg_match('/\d{4}/', $string);
This will return 1
if the string contains a 4-digit year, or 0
if it does not.
The code consists of the following parts:
preg_match()
: This is a PHP function that performs a regular expression match./\d{4}/
: This is the regular expression pattern used to match a 4-digit year. The\d
character class matches any digit, and the{4}
quantifier specifies that exactly 4 digits must be matched.$string
: This is the string that will be searched for a 4-digit year.
For more information on using regular expressions in PHP, see the PHP documentation.
More of Php Regex
- How to match the end of a string when using regex in PHP?
- How to use PHP regex to match special characters?
- How to use PHP regex to match an exact string?
- How to use PHP regex to match a nbsp HTML whitespace?
- How to use PHP regex to match a zip code?
- How to use PHP regex to match UTF8?
- How to use PHP regex to get a YouTube video ID?
- How to use PHP regex with the "x" modifier?
- How to use PHP regex to match an XML tag?
- How to use PHP regex to match URL path?
See more codes...