0

How would I formulte the pattern so I would extract the XXXX-XX-XX out of a time tag?

The line I'm searching for in a $string is this:

<time datetime="XXXX-XXX-XX" itemprop="birth">

And I want to extract the part:

XXXX-XX-XX

I have this but it's not working:

preg_match('\<time datetime="d{4}-d{2}-d{2}"',$string,$date);
echo $date[0] . "<br />";

I find regex expressions so damn confusing ... any good tutorial recommendation would also be great! Appreciate your time and help.

Cheers

Afonso Gomes
  • 902
  • 1
  • 14
  • 40

3 Answers3

3

You need to use \d in place of just d.

Also you need to enclose the regex in pair of delimiter.

Like:

preg_match('/\<time datetime="\d{4}-\d{2}-\d{2}"/',$string,$date);
codaddict
  • 445,704
  • 82
  • 492
  • 529
2

You forgot the delimiters, some escapes and a capturing group:

preg_match('/<time datetime="(\d{4}-\d{2}-\d{2})"/',$string,$date);
echo $date[1] . "<br />";
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
1

Your pattern needs the /../

preg_match('/\<time datetime="d{4}-d{2}-d{2}"/',$string,$date);

Edit: See tim's answer, you need the capture group too.

Michael Dillon
  • 1,037
  • 6
  • 16