0

我有一个数组,其中包含以下值之一

<meta itemprop="datePublished" content="Mon Mar 04 08:52:45 PST 2013"/>

我如何从那里提取 2013 年 3 月 4 日?这是一个动态的领域,并且永远在变化。我似乎找不到正确的方法

我希望能够回显 $datepub; 只是有日期。

谢谢

4

2 回答 2

1

一个非常简单的方法可能是爆炸它:

//dividing the string by whitespaces
$parts = explode(' ', $datepub);  

echo $parts[1]; //month (Mar)
echo $parts[2]; //day (04)
echo $parts[5]; //year (2013)

然后您可以使用createFromFormat函数将其转换为任何其他所需的格式:

//creating a valid date format
$newDate = DateTime::createFromFormat('d/M/Y', $parts[1].'/'.$parts[2].'/'.$parts[5]);

//formating the date as we want
$finalDate = $newDate->format('F jS Y'); //March 4th 2013
于 2013-03-12T16:12:25.263 回答
0

使用SimpleXML使用代码示例扩展Marc B的答案:

$data = '<?xml version="1.0"?><meta itemprop="datePublished" content="Mon Mar 04 08:52:45 PST 2013"/>'; // your XML
$xml = simplexml_load_string($data);

// select all <meta> nodes in the document that have the "content" attribute
$xpath1 = $xml->xpath('//meta[@content]');
foreach ($xpath1 as $key => $node) {
    echo $node->attributes()->content; // Mon Mar 04 08:52:45 PST 2013
}

// Marc B's select "content" attribute for all <meta> nodes in the document
$xpath2 = $xml->xpath('//meta/@content');
foreach ($xpath2 as $key => $node) {
    echo $node->content; // Mon Mar 04 08:52:45 PST 2013
}
于 2013-03-12T16:17:27.277 回答