Best way to check RSS item URLs for string and echo source name?

Hi :slight_smile: I am trying to alter a RSS module for Joomla to give the name of the source of the item. right now the code for this operation is pasted below. however, i do not think this is the most efficient way to do this. is there a way that require less resources from the server? i may have 50 or so sources, not just the ones in the example.

[php]

<?php if (strpos($feed->itemLink, "espn") !== false) { echo "ESPN -"; } ?> <?php if (strpos($feed->itemLink, "cnn.com") !== false) { echo "CNN -"; } ?> <?php if (strpos($feed->itemLink, "sportsillustrated") !== false) { echo "Sports Illustrated -"; } ?> <?php if (strpos($feed->itemLink, "nbcsports") !== false) { echo "NBC -"; } ?> <?php if (strpos($feed->itemLink, "indystar.com") !== false) { echo "The Indianapolis Star -"; } ?> <?php if (strpos($feed->itemLink, "cbsnews.com") !== false) { echo "CBS -"; } ?> <?php if (strpos($feed->itemLink, "bloomberg.com") !== false) { echo "Bloomberg -"; } ?> <?php if (strpos($feed->itemLink, "youtube") !== false) { echo " YouTube -"; } ?> <?php if (strpos($feed->itemLink, "nytimes") !== false) { echo " The New York Times -"; } ?>[/php]

I would just use an array! Something like…

$sourceArray = array(“cnn”, “espn”, “msn”, “blah”, “blah”);
foreach ($sourcearray as &$source) {
$temp = strpos($feed->itemLink, $source)
if (strpos($feed->itemLink, $source) !== false)
echo $source . " -";
}

You would have to make a second array to hold the translated versions of the sources,
but, I think you get the idea of where to head…

Thank you for your reply. Sorry, I am very unfamiliar with arrays… I am not clear on how to integrate the second array to hold the official names of the sources. ::slight_smile:

Well, I gave you the general layout of how to do it. For future knowledge, you can look at PHP.net and find out how all functions work for PHP. Here is a link for searching arrays on their site. Read the notes where user’s have posted samples. http://www.php.net/manual/en/function.array-search.php But, to save you some time, here is a code sample that will make it easier for now…
[php]

<?PHP $sourceArray = array(0 => "cnn", 1 => "espn", 2 => "msn", 99 = >"blah..etc.."); $sourceName = array("C.N.N.", "E.S.P.N", "Microsoft Network", "continue from here..."); foreach ($sourceArray as $key=>$source) { //Loop thru possible sources if (strpos($feed->itemLink, $source) !== false) //Check if it is in current feed... echo $sourceName[$key] . " -"; //$key is position in matching arrays }else{ echo "Unknown Source! - "; // or code to notify you of unknown source! } ?>

[/php]
I think something like that should work. Please note, that the two arrays must match each other. The key in the first one points to the entry in the second one. This is a simple trick used to convert items by use of arrays. Also, spelling is important as capitals are used. So, “CNN” is NOT “cnn”. You can use tricks to fix that. You can use (strpos(strtolower($feed->itemLink), $source) for your compare. This will convert the entire itemlink in $feed to be lower case for the testing. If you use that, you should make sure all of the first array is entered in lower case. Hope that makes sense and hope it helps…
Good luck…

Sponsor our Newsletter | Privacy Policy | Terms of Service