07-14-2008, 03:56 AM
|
#7 (permalink)
|
|
is cute and cuddly
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
|
I stripped a lot of your formatting out to debug this, and this is what I came up with that works just fine (without styles):
PHP Code:
<?php
$contents = file_get_contents('http://youtube.com/rss/global/top_rated.rss'); $xml = new SimpleXMLElement($contents);
$rss_count = 0;
$desclimit = 225; // use 0 for no limit
foreach ($xml->channel->item as $item) { $img = preg_replace('/^.*?(<img [^>]+>).*$/is','\1',$item->description); // Uncomment if you need full <img /> tag $imgsrc = preg_replace('/^.*?(<img [^>]*?src="([^"]+)"[^>]*>).*$/is','\2',$item->description); $tmp_desc = preg_replace('#.*?(<p>.+?</p>).*#is','\1',$item->description); $tmp_desc = trim(preg_replace('#(<[^>]+>|http://\S+|www\.\S+\.\S{2,3}\S+)#','',$tmp_desc));
$linkarray = explode("=",$item->link);
echo '<a href="',$item->link,'"><img src="http://s2.ytimg.com/vi/',$linkarray[1],'/default.jpg" title="$item->title." style="float: left;" /></a>'; echo '<h2><a href="',$item->link,'">',$item->title,'</a></h2>';
echo '<p style="clear: both;">'.((($desclimit != 0) && (strlen($tmp_desc) > $desclimit)) ? substr($tmp_desc,0,$desclimit-3)."..." : $tmp_desc).'</p>';
$rss_count++;
if (isset($_GET['limit']) && (int) $_GET['limit'] == $rss_count) break;
}
?>
<b><center>Youtube RSS Grabber 0.1 BETA - By Codefreek</center></b>
</br> <center><b>NEWS</b> <ol> </br> <p>now you get The video Title when you hover over a tumbnail. more small changes will be made.</p></ol></center>
</br>
<center><a href="http://www.td4l.org"><p>This site is hosted by: Td4l.org</p></a></center>
<?php echo '<form action="',$_SERVER['PHP_SELF'],'" name="rss" method="get">'; echo '<input type="submit" name="subten" value="subten">'; echo '<input type="submit" name="subfive" value="subfive">'; echo '<input type="submit" name="defualt" value="defualt">'; echo '</form>'; ?>
This is just a stripped down version of yours, not my own version. A few things I came across:
- You had a few variables you weren't using. $limit and $i, which you do a check on further down, but nowhere do you increment these values or give them a reason to exist. They seemed sad, so I let them go home.
- Your post if/else structure to determine the amount of results returned was a little convoluted. I switched it to a simple GET method so you only have to run one check.
- Your string replacement routines (preg_replace) were trying to run on $item which doesn't exist outside of the foreach, so I moved them.
- Your $desclimit reality check isn't really necessary, you should just add a comment in to the script if you want, that says something along the lines of "If you enter this amount any lower than 45 characters, the description returned will be too short to be readable"... or something.
- Finally your description limiter wasn't working because you were echoing $item->description directly, and not $str which is where you were performing the truncate.
|
|
|
|