Using preg_match to search through rss feed

Discussion in 'PHP' started by lruneh, Jan 2, 2009.

  1. #1
    Hey all,

    I have an rss feed that is stored in a database. I'm trying to search through this feed using preg_match(). What I want to do is extract all the items that contain my search term.

    So I guess what I have to do is match /<item> [whatever] $term [whatever] </item>/. I've been trying to get a regular expression working, but with no luck...

    I'd really appreciate any help you can give me. Thanks
     
    lruneh, Jan 2, 2009 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    If it's an XML feed couldn't you use just SimpleXML and a basic loop/array mashup to get the information you need?

    Dan.
     
    Danltn, Jan 2, 2009 IP
  3. lruneh

    lruneh Peon

    Messages:
    72
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hey and thanks a lot for your answer

    Wouldn't it be just as quick to simply search through it with a preg_match? Even if I use simpleXML, I'll still need to do the search...
     
    lruneh, Jan 2, 2009 IP
  4. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #4
    <?php
    
    $url = 'http://www.wiredbyduffy.com/rss/wbdrss.xml';
    $term = 'Sudoku';
    $insensitive = true;
    /* Example */
    
    $feed = strtr(file_get_contents($url), "\t\n\r", '   ');
    preg_match_all('/<item>(.*?'.$term.'.*?)<\/item>/' . ($insensitive ? 'i' : ''), $feed, $items);
    
    print_r($items); //  Example output.
    
    ?>
    PHP:
     
    Danltn, Jan 2, 2009 IP
    lruneh likes this.
  5. lruneh

    lruneh Peon

    Messages:
    72
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Nothing short of brilliant! Thank you very much!
     
    lruneh, Jan 2, 2009 IP
  6. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #6
    Well here's what I do to handle rss feeds.
    
    $rss = simplexml_load_file('http://www.site.com/rss.php');
    foreach ($rss->channel->item as $x) {
        $x = (Array) $x; // Don't like handling objects, typecast to array
        // Do your thing here
    }
    
    PHP:
     
    Kaizoku, Jan 2, 2009 IP