Mike Thrussell
Mike Thrussell

Reputation: 4515

preg-match date in 1 Jan 2001 format

I'm wish to remove all content in a string after this phrase in PHP:

On 17 Mar 2015

How would I match any date?

EDIT: I use this currently to remove the phrase and all content after it in a string:

if (strpos($body,"On 17 Mar 2015") !== false) { $body = substr($body, 0, strpos($body, "On 17 Mar 2015"));}

Input =

reply reply reply reply reply reply reply reply 

On 17 Mar 2015, at 10:23, user wrote:

> email email email email email email email email email email email email 
> email email email email email email email email email email email email

Expected output:

reply reply reply reply reply reply reply reply

for any possible date

Upvotes: 0

Views: 70

Answers (1)

jvitasek
jvitasek

Reputation: 810

If the format of the phrase doesn't change, the regular expression below should do the trick for any date possible.

$phrase = "On 17 Mar 2015";
preg_match("#(?<=On\s)\d+\s\w+\s\d+#", $phrase, $matches);
echo $matches[0];

Upvotes: 1

Related Questions