0

I have some Information which i have parsed from the Wikipedia Infobox using the Wikipedia API. One of my Information which i want to parse is the birthdate:

$birthdate = "birth_date = {{Birth date|1958|8|29}}"

i want have the birtdate information that the output is like this:

1958-8-29

in my considerations i can use the php function explode but i think this is a very bad way to get in each case the right information back.

My Question is:

Does anyone know a way to parse the birthdate information in each case right, independently from the other content? And if yes, can you make a example?

R2D2
  • 743
  • 1
  • 7
  • 14

1 Answers1

0

You can try this one with regular expression.

$matches = array();
$birthdate = "birth_date = {{Birth date|1958|8|29}}";
preg_match('/^(.*)\|([0-9]{4})\|([0-9]{1,2})\|([0-9]{1,2})}}/is', $birthdate, $matches);
if (!empty($matches[2]) && !empty($matches[3]) && !empty($matches[4])) {
    echo $matches[2]."-".$matches[3]."-".$matches[4];
} else {
    echo 'Not found the parts of the date';
}
//Output is: 1958-8-29 
vaso123
  • 12,347
  • 4
  • 34
  • 64