如何使用字符串上的preg匹配来提取日期

时间:2016-03-10 16:24:53

标签: php preg-match

如何从这两个字符串中提取日期。

Date: 03/03 - 14:10

text

来自

    Date/Time: 08/03/16 13:50

Summary of Fault: 

我试过了

$matches = array();
                $pattern = "/^.*\Date\/Time\:\b.*$/m";
                preg_match($pattern, $toParse, $matches);
                echo($matches[0]);

                $matches = array();
                $pattern = "/.*Date:.*/mi";
                preg_match($pattern, $toParse, $matches);

2 个答案:

答案 0 :(得分:1)

首先,为了提取所有匹配的事件,您需要使用preg_match_all()时间模式:

/.[0-9]:.[0-9]/

Patern for Date:

/.[0-9]\/.[0-9]\/.[0-9]/

以下是对preg_match_all

的引用

答案 1 :(得分:0)

你必须使用preg_match_all

'/: (.*?) /'

https://regex101.com/r/tY6bT5/1