我正在閱讀RSS源並將其輸出到頁面上,並且我需要獲取<description>
標記的子字符串並將其存儲爲變量(然後將其轉換爲其他時間格式,但我可以自己弄清楚)。這裏是我正在使用的數據樣本:使用PHP獲取字符串的一部分
<description><b>When:</b> Tuesday, November 03, 2015 - 6:00 PM - 8:00 PM<br><b>Where:</b> Adult Literacy Classroom (Lower Level) dedicated in honor of Eleanor Moore<br><br>Clases de preparación para el GED – grupos de estudio para ayudar con sus habilidades y preparación para obtener su diploma de equivalencia de escuela. Las clases se llevaran a cabo en español, según la materia (escritura, literatura, estudios sociales, ciencias, matemáticas y la constitución) <br /><br />GED preparation classes – Study groups to help build your skills that will prepare you to get your high school equivalency diploma. Classes are taught in Spanish by subject area (writing, literature, social studies, science, math and the constitution)<br /></description>
我已經得到了描述標籤內的所有內容作爲varible,我只需要搶串Tuesday, November 03, 2015 - 6:00 PM - 8:00 PM
,但我無法弄清楚如何要做到這一點。我有一種感覺PHP的explode
可能會工作,但我與正則表達式很糟糕。我會繼續努力併發回我的進度,但任何幫助將不勝感激。
順便說一句,我用這個方法來獲取數據:http://bavotasan.com/2010/display-rss-feed-with-php/
感謝@ Bomberis123,我能夠做的正是我需要的。我的代碼可能會有點亂,但我想我會分享它的人誰需要做同樣的事情:
<?php
$next_up_at_rss_feed = new DOMDocument();
$next_up_at_rss_feed->load("http://host7.evanced.info/waukegan/evanced/eventsxml.asp?ag=&et=&lib=0&nd=30&feedtitle=Waukegan+Public+Library%3CBR%3ECalendar+of+Programs+%26+Events&dm=rss2&LangType=0");
$next_up_at_posts = array();
foreach ($next_up_at_rss_feed->getElementsByTagName("item") as $node) {
$date = preg_match("/((\s)([^\<])+)/", $node->getElementsByTagName("description")->item(0)->nodeValue, $matches, PREG_OFFSET_CAPTURE, 3);
$date = $matches[0][0];
$next_up_at_post = array (
"title" => $node->getElementsByTagName("title")->item(0)->nodeValue,
"date" => $date,
"link" => $node->getElementsByTagName("guid")->item(0)->nodeValue,
);
array_push($next_up_at_posts, $next_up_at_post);
}
$next_up_at_limit = 4;
for ($next_up_at_counter = 0; $next_up_at_counter < $next_up_at_limit; $next_up_at_counter++) {
// get each value from the array;
$title = str_replace(" & ", " & ", $next_up_at_posts[$next_up_at_counter]["title"]);
$link = $next_up_at_posts[$next_up_at_counter]["link"];
$date_raw = $next_up_at_posts[$next_up_at_counter]["date"];
// seperate out the date so it can be formatted
$date_array = explode(" - ", $date_raw);
// set up various formats for date
$date = $date_array[0];
$date_time = strtotime($date);
$date_iso = date("Y-m-d", $date_time);
$date_pretty = date("F j", $date_time);
// set up various formats for start time
$start = $date_array[1];
$start_time = strtotime($start);
$start_iso = date("H:i", $start_time);
$start_pretty = date("g:ia", $start_time);
// set up various formats for end time
$end = $date_array[2];
$end_time = strtotime($end);
$end_iso = date("H:i", $end_time);
$end_pretty = date("g:ia", $end_time);
// display the data
echo "<article class='mini-article'><header class='mini-article-header'>";
echo "<h6 class='mini-article-heading'><a href='{$link}' target='_blank'>{$title}</a></h6>";
echo "<p class='mini-article-sub-heading'><a href='{$link}' target='_blank'><time datetime='{$date_iso}T{$start_iso}-06:00'>{$date_pretty}, {$start_pretty} - {$end_pretty}</time></a></p>";
echo "</header></article>";
}
?>
請張貼代碼在這裏 – 2015-11-03 20:30:27
謝謝了,這是完美的:) – JacobTheDev
很高興我幫助,乾杯;)@Rev – Bomberis123