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
  • 872
  • 1
  • 13
  • 37

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
  • 429,241
  • 80
  • 483
  • 523
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
  • 313,408
  • 56
  • 485
  • 544
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,027
  • 6
  • 16