用php识别字符串中的日期

Osc*_*lli 1 php time datetime date date-parsing

我有两个不同的字符串:

  • 宝贝14%28.07.2012
  • 你好2%15.06.16十

("宝贝"或"14%"或"十"可以改变,所以我不能指定它们)

我想识别该字符串中的日期,我试过:

$dt = date_parse($string);
Run Code Online (Sandbox Code Playgroud)

在第一种情况下,没关系,但在第二种情况下,它无法识别日期.

我试过没有成功

$dt = date_parse_from_format("d.m.Y", $string);
Run Code Online (Sandbox Code Playgroud)

我尝试使用preg_match或preg_match_all但没有成功(我不是很擅长).

ber*_*rty 6

您可以使用正则表达式捕获日期,例如:

if(preg_match('/(.*)([0-9]{2}\.[0-9]{2}\.[0-9]{2,4})(.*)/', $yourString, $matches))
{
 $date = $matches[2];
}
Run Code Online (Sandbox Code Playgroud)