0

I have an rss feed that I am reading into. I need to retrieve certain data from the field in this feed.

This is the example feed data :

<content:encoded><![CDATA[
    <b>When:</b><br />
    Weekly Event - Every Thursday: 1:30 PM to 3:30 PM (CT)<br /><br />
      <b>Where:</b><br />
      100 West Street<BR>2nd floor<BR>Gainesville<BR>
      <br>.....

How do I pull out the data for When: and Where: respectively? I attempted to use regex but I am unsure if I am not accessing the data correctly or if my regex expression is wrong. I'm not set on using regex.

This is my code:

    foreach ($x->channel->item as $event) {
    $eventCounter++;
    $rowColor = ($eventCounter % 2 == 0) ? '#FFFFFF' : '#F1F1F1';
    $content = $event->children('http://purl.org/rss/1.0/modules/content/');
    $contents = $content->encoded;
    echo '<tr style="background-color:' . $rowColor . '">';
    echo '<td>';
    //echo "<a id=buttonRed href='$event->link' title='$event->title' target='_blank'>" . $event->title . "</a>";
    echo "" . $event->title . "";
    echo '</td>';
    echo '<td>';
    $re = '%when\:\s*</b>\s*(.|\s)<br \/><br \/>$/i';
    if (preg_match($re, $contents, $matches)) {
        $date = $matches;
    }
    echo $date;
    echo '</td>';
    echo '<td>';
    $re = '/^When\:<\/b>()$/';
    if (preg_match($re, $contents, $matches)) {
        $location = $matches;
    }
    echo $location;
    echo '</td>';
    echo '<td>';
    echo "<a id=buttonRed href='$event->link' title='$event->title' target='_blank'>Click Here To Register</a>";
    echo '</td>';
    echo '</tr>';
}

The two $res are just my attempt to get the data out using different regex expressions. Let me know where I am going wrong. Thanks

Jonathan M
  • 17,145
  • 9
  • 58
  • 91
wiseman7687
  • 175
  • 1
  • 2
  • 13
  • Don't downvote him. He says he's not married to the regex approach. Just show him how you'd do it. – Jonathan M Feb 08 '12 at 17:49
  • Thanks, as Jonathan said. I am not saying I have to use regex. I tried some strpos as well first but it wasn't loading correctly. I just want to know how to get those certain fields of data. – wiseman7687 Feb 08 '12 at 17:53
  • @JonathanM: He was told that before. http://stackoverflow.com/questions/7969090/preg-match-help-cannot-read-string-from-email – mario Feb 08 '12 at 17:57

3 Answers3

1

I had a problem like this and I ended up using YQL. Take a good look at the page-scraping code given there, especially the select command. Then go the the console and put in your own select statement, specifying the feed url and the xpath to the nodes you're wanting. Select JSON format. Then go down to the bottom of the page, get the REST query url, and use it in a jquery jsonp request. MAGIC!

Jonathan M
  • 17,145
  • 9
  • 58
  • 91
1

The following should sort of get you there. (I wrote this from the top of my head and it does not exactly following your XML syntax. But you get the idea.)

<?php
$str = "<root><b>When:</b> whenwhen <b>Where:</b> wherewhere</root>";

$doc = new DOMDocument();
$doc->loadXML($str);

$when = $where = "";
$target = null;

foreach ($doc->documentElement->childNodes as $node) {
    if ($node->tagName == "b") {
        if (++$i == 1) {
            $target = &$when;
        } else {
            $target = &$where;
        }
    }

    if ($target !== null && $node->nodeType === XML_TEXT_NODE) {
        $target .= $node->nodeValue;
    }
}

var_dump($when, $where);
Martin Jansen
  • 276
  • 1
  • 7
-2

please, don't extract data from XML-documents via regex.

The long answer is e.g. here: https://stackoverflow.com/a/335446/313145

The short answer is: it is not easier to use regex and will break often.

Community
  • 1
  • 1
Jörg Beyer
  • 3,631
  • 21
  • 35
  • I did not want to use regex, I just could not retrieve my data properly so this was my latest attempt. What do you recommend? – wiseman7687 Feb 08 '12 at 17:37
  • 1
    perhaps this would have been better suited as a comment, rather than an answer? – Ayush Feb 08 '12 at 17:38