2013-03-12 98 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 
}