Extract the URL of a RSS feed from source code with Regex

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • emnia
    New Member
    • Jul 2008
    • 2

    Extract the URL of a RSS feed from source code with Regex

    Can anybody help me with this questions? It's probably easy for you...

    Example:
    Code:
    <link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="http://site.com/feed.rss" />
    I need the href value from that string. I tried many Regex examples but they did not work so good.

    Any hints would be welcome!
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    I would think something like this would work:
    [code=php]
    $success = preg_match('/\<link.*href=\" (.*)\" \/\>/is', $str, $matches);
    [/code]
    To break that down, we have the opening "<link", followed by any number of any character, until it reaches a href=", from which point we want any number of any characters, until we reach the closing ", followed by the closing />

    Comment

    • emnia
      New Member
      • Jul 2008
      • 2

      #3
      Thanks! It worked great!

      Thanks to you I can now improve my site thanks!
      I used it on a link directory site for Kazakh sites, yeah right, (www.REMOVED-BY.MOD.) to grab the RSS url if the user forgot to enter it.

      This will help many others I think!

      PS. My function looks like this now:

      [PHP]function grab_rss_url($u rl){

      $text = @file_get_conte nts($url);
      $text = substr($text,0, 3000);

      $success = preg_match('/\<link.*href=\" (.*)\" \/\>/is', $text, $matches);

      return $matches[1];

      }[/PHP]
      Last edited by ak1dnar; Aug 7 '08, 05:47 PM. Reason: Promoting Commercial web site not allowed here

      Comment

      Working...