PHP regex expression to find if a string contains YYYY-MM-DD
date, php, regex, string
Solution
if (preg_match('/\b\d{4}-\d{2}-\d{2}\b/', $str)) {
// ...
}
If the word boundary (`\b`) doesn't do the trick, you could try negative lookbehind and lookaheads:
if (preg_match('/(?<!\d)\d{4}-\d{2}-\d{2}(?!\d)/', $str)) {
// ...
}
As an additional validation, you could use `checkdate()` to weed out invalid dates such as `9999-02-31` as mentioned in this answer.
Problem
I would like to check if a URL (or any string) contains any form of the following pattern ####-##-## Does anyone have a handy str_replace() or regular expression to do that? something like: ``` contains_date_string($string); ``` returns true if $string contains ####-##-## Thank you!!!