I'm reading an RSS feed and outputting it on a page, and I need to take a substring of the <description> tag and store it as a variable (and then convert to a different time format, but I can figure that out myself). Here's a sample of the data I'm working with:
<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>
I've already got everything within the description tag as a varible, I just need to grab the string Tuesday, November 03, 2015 - 6:00 PM - 8:00 PM, but I can't figure out how to do that. I have a feeling PHP's explode might work, but I'm terrible with regex. I'll keep working on it and post back my progress, but any help would be greatly appreciated.
By the way, I'm using this method to get the data: http://bavotasan.com/2010/display-rss-feed-with-php/
Thanks to @Bomberis123, I was able to do exactly what I needed to. My code may be a little messy, but I figured I'd share it for anyone who needs to do something similar:
<?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>";
}
?>