RSS Feed into PHP

Discussion in 'PHP' started by Bruny07, May 9, 2011.

  1. #1
    Hi guys, I'm kinda new in PHP and I ran into a problem.
    I have a script to read a RSS Feed into PHP and I want to get just a part from <link></link>

    This is the functions.php
    <?php
    
    function getFeed($feed_url) {
    	
    	$content = file_get_contents($feed_url);
    	
    	$x = new SimpleXmlElement($content);
    	
    	echo "<ul>";
    	
    	foreach($x->channel->item as $entry) {
    		echo "
    		<li>
    		  <a href='$entry->link' title='$entry->title'>" . $entry->title . "</a>
    		</li>";
    		}
    	echo "</ul>";
    }
    ?>
    PHP:
    and in index.php I have
    <?php getFeed("http://www.tvrage.com/myrss.php?class=scripted"); ?>
    PHP:
    In that feed the link is like <link>http://www.tvrage.com/Chuck</link> and I wonder if its possible to remove http://www.tvrage.com/ and use only Chuck.


    Thanks
     
    Bruny07, May 9, 2011 IP
  2. ntomsheck

    ntomsheck Peon

    Messages:
    87
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    preg_replace("<link>http:\/\/www\.tvrage\.com\/([A-Za-z]+)<\/link>","$1",$whichevervariablehasthelink);

    This will turn <link>http://www.tvrage.com/RegexIsAwesome</link> into RegexIsAwesome
     
    ntomsheck, May 9, 2011 IP
  3. Bruny07

    Bruny07 Member

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    I get an error

    Warning: preg_replace() [function.preg-replace]: Unknown modifier 'h' in C:\xampp\htdocs\test\2\index.php on line 26
    Code (markup):
    preg_replace("<link>http:\/\/www\.tvrage\.com\/([A-Za-z]+)<\/link>","$1",$feed);
    PHP:
     
    Bruny07, May 10, 2011 IP
  4. dmvictoria

    dmvictoria Well-Known Member

    Messages:
    400
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    140
    #4
    $1 variable cannot start with a numeral.

    http://www.w3schools.com/PHP/php_variables.asp
     
    dmvictoria, May 10, 2011 IP
  5. Bruny07

    Bruny07 Member

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #5
    I figured it out

    Thanks for the help
     
    Bruny07, May 10, 2011 IP
  6. Bruny07

    Bruny07 Member

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #6
    One more question. If I use this script to get the feed, it would work on a shared hosting?
     
    Bruny07, May 10, 2011 IP
  7. dmvictoria

    dmvictoria Well-Known Member

    Messages:
    400
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    140
    #7
    Most (if not all) shared hosting is running PHP, its very common place.

    Would you mind telling us what you did to fix it?
     
    dmvictoria, May 10, 2011 IP
  8. Bruny07

    Bruny07 Member

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #8
    I know that they are running php but how about server load? If I will get more visitors will not affect the server?

    I've added slashes
    $y = preg_replace("/http:\/\/www\.tvrage\.com\/([A-Za-z]+)/","$1",$entry->link);
    		echo "<img src='shows/$y.jpg' title='$entry->title' />";
    PHP:
     
    Bruny07, May 10, 2011 IP
  9. dmvictoria

    dmvictoria Well-Known Member

    Messages:
    400
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    140
    #9
    Most servers allow 25 simultaneous mysql connections, that is high traffic. Shared hosting can take quite a bit of traffic. A colleague of mine had a 40,000 unique serge in a day and there was no discernible problems. And thats just calls to a data base, not all php makes a request to a DB and generally there are no limits on just scripts that don't request DB info. Ultimately, check with your host for limitations.

    Who is your host?
     
    dmvictoria, May 10, 2011 IP
  10. Bruny07

    Bruny07 Member

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #10
    For now I don't have a host but I think I choose hostgator. If you know other good hosts please let me know.
     
    Bruny07, May 11, 2011 IP
  11. esauldaris

    esauldaris Peon

    Messages:
    946
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Maybe magpie is one of the best tool to convert rss into a sitepage.
     
    esauldaris, May 11, 2011 IP
  12. ntomsheck

    ntomsheck Peon

    Messages:
    87
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #12
    That's less a PHP variable and more a perl regex variable. Read about preg_ functions too - any info that matches whatever is within a parenthesis set is turned into a numeric variable of a number related to the order in which it was presented. But someone else could probably write that less confusingly...
     
    ntomsheck, May 14, 2011 IP
  13. dmvictoria

    dmvictoria Well-Known Member

    Messages:
    400
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    140
    #13
    My first feed parser was Magpie and it was great. But, its a heavy script with many limitations, as the more PHP I learned I eventually dropped Magpie in favor of my own custom scripts.

    Yeah, I've been reading a little more about that function. After wrapping my head a little more I'll be using it more often.
     
    dmvictoria, May 14, 2011 IP