2013-03-12 88 views
0

的某一部分我有一个包含以下作为它的一个人的价值观提取字符串

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

我如何提取的2013年3月4日离开那里的阵列?这是一个充满活力的领域,并且一直在变化。我似乎无法找到正确的方法来做到这一点

我想只能echo $ datepub;只有日期。

感谢

+2

xml?使用dom + xpath:'// meta/@ content' – 2013-03-12 16:00:19

+0

您可以使用正则表达式来提取内容之间的内容=“Mon Mar 04 08:52:45 PST 2013”​​ – bodi0 2013-03-12 16:03:23

回答

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 
+0

谢谢!我不敢相信我没有想到爆炸。我最终不得不稍微调整一下以下$ idate = explode('',$ i ['date']); $ newDate =($ idate [3]。''.idate [4]。''.str_replace(“\”/>“,”“,$ idate [7]));但工作完美 – 2013-03-12 16:51:06

+0

欢迎您光临。 :) – Alvaro 2013-03-12 16:58:23

0

要使用SimpleXML

012扩展 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 
}