views:

75

answers:

3

I need to find the value of a variable and use it to add a class to a div, based on a switch statement.

For example, my variable is $link and if $link has google.com IN IT at all, I need $class to equal 'google', if $link as yahoo.com IN IT at all, $class then needs to equal 'yahoo'

So, I need something like this, but I'm not sure how/or if to use preg_match or something to check and see if the $link variable has the value we are looking for in it - see 'case' text below:

    switch ($link) {
        case 'IF link has Google.com in it':
                        $class = 'google';
            break;

        case 'IF link has Yahoo.com in it':
                        $class = 'yahoo';
            break;

        default:
            # code...
            break;
}

OR if there is a better way to do this, please let me know :D

Also, I'm good with using an IF ELSE statement as well..

Thanks

+3  A: 

You want an IF-statement, not a switch statement

Joe Philllips
+2  A: 

I think preg_matchis not necessary here.stripos is enough for it.

$url = $link->hits;
$pos_google = stripos($url,'google.com');
$pos_yahoo = stripos($url,'yahoo.com');
if($pos_google !== false)
{
     $class = 'google';
}
elseif($pos_yahoo !== false)
{
     $class = 'yahoo';
}
else
{
     #code
}
SpawnCxy
Let me rephrase, and please bear with me, I am a UI/Designer trying to pick up the slack.. if you echo $link->hits you get a URL.. I just need to check to see if that returned URL has google.com in it, if so, set $class to 'google', same with yahoo.com, etc.. :D Hope that helps.
revive
@revive,OIC,see the update:)
SpawnCxy
Perfect!!! Thank you @SpawnCxy !!
revive
@revive,u're welcome.I would be more happier if you can accept the answer:-)
SpawnCxy
I've tried!! Stackoverflow wont let me ! It keeps saying I have to have a rep of 15 or higher to vote?!?!?!? can someone else click this for me, please??!
revive
@revive,there're two actions for you to do with an answer:voting and accepting.u cannot voting as the limit of reputation now,but accepting is available by clicking the tick below the voting button.
SpawnCxy
A: 

Seems it could be simpler:

if(ereg("google", $link)){
    $class = "google";
}else if(ereg("yahoo", $link)){
    $class = "yahoo";
}else{
    $class = "";
}
jerebear
I'd recommend using the `preg_` regex functions, as the ereg family are deprecated http://devthought.com/tumble/2009/06/fix-ereg-is-deprecated-errors-in-php-53/
alex