If statement help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
divito
Forum Commoner
Posts: 89
Joined: Sun Feb 22, 2009 7:29 am

If statement help

Post by divito »

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.
divito
Forum Commoner
Posts: 89
Joined: Sun Feb 22, 2009 7:29 am

Re: If statement help

Post by divito »

Updated.
semlar
Forum Commoner
Posts: 61
Joined: Fri Feb 20, 2009 10:45 pm

Re: If statement help

Post by semlar »

If you're just searching for a word in a string you can use strpos to find it.

Code: Select all

if(strpos($url,'section')) { do things }
If you need more versatile matching you can use regular expressions via preg_match.
Last edited by semlar on Mon Feb 23, 2009 1:26 pm, edited 1 time in total.
divito
Forum Commoner
Posts: 89
Joined: Sun Feb 22, 2009 7:29 am

Re: If statement help

Post by divito »

Here is the code:

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>";
}
    
?>
I've tried so many different variations and alterations, including using preg_match and strstr. I'm sure I'm making a silly mistake.
semlar
Forum Commoner
Posts: 61
Joined: Fri Feb 20, 2009 10:45 pm

Re: If statement help

Post by semlar »

It doesn't look like you have a closing bracket (}) for your foreach, you're not getting an error?
divito
Forum Commoner
Posts: 89
Joined: Sun Feb 22, 2009 7:29 am

Re: If statement help

Post by divito »

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. :banghead:

Thanks a lot.
semlar
Forum Commoner
Posts: 61
Joined: Fri Feb 20, 2009 10:45 pm

Re: If statement help

Post by semlar »

It doesn't help that your indents are a little misleading :P

It's always the little things.
Post Reply