Hello all. I am not exactly new to PHP, but in relation to anything requiring an intermediate to advanced level knowledge of it, you could say I'm a moron.
When putting together my RSS feeds for certain sites, some do not delineate their different sections of news leaving a large clump of returns; whereas other sites have it and I can group them accordingly.
I was wondering how I would go about putting together an if statement for checking whether $url contains a certain word to help me to output to a group properly.
For instance, http://www.link.com/section/title - I want to setup an if statement to check if $url contains "section" and output it properly; and I'll be having more ifs for the other various sections.
Thanks.
If statement help
Moderator: General Moderators
Re: If statement help
If you're just searching for a word in a string you can use strpos to find it.
If you need more versatile matching you can use regular expressions via preg_match.
Code: Select all
if(strpos($url,'section')) { do things }
Last edited by semlar on Mon Feb 23, 2009 1:26 pm, edited 1 time in total.
Re: If statement help
Here is the code:
I've tried so many different variations and alterations, including using preg_match and strstr. I'm sure I'm making a silly mistake.
Code: Select all
<?php
require_once 'magpierss-0.72/rss_fetch.inc';
$url = 'http://www.tsn.ca/datafiles/rss/Stories.xml';
$rss = fetch_rss($url);
foreach ($rss->items as $item ) {
$title = $item[title];
$url = $item[link];
$nhl = 'nhl';
$test = strpos($url, $nhl);
if ($test == true) {
echo "NHL:<a href=$url>$title</a></li><br>";
} else {
echo "<a href=$url>$title</a></li><br>";
}
?>Re: If statement help
It doesn't look like you have a closing bracket (}) for your foreach, you're not getting an error?
Re: If statement help
I knew it was something silly though. That's exactly what it was. I guess when I copied and pasted some of my code over, I accidentally highlighted that last bracket.
Thanks a lot.
Thanks a lot.
Re: If statement help
It doesn't help that your indents are a little misleading :P
It's always the little things.
It's always the little things.