0

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

4
  • What is $link, a string, an array? Commented Apr 23, 2010 at 3:07
  • It's an array, actually, it's $link->hits
    – revive
    Commented Apr 23, 2010 at 3:36
  • 1
    Can you provide how that array stores that information? In terms of the content. Commented Apr 23, 2010 at 3:39
  • Wish I could.. the dev is not avail. and I am trying to finish adding the class based in the result of the variable.. it is built on CodeIgniter 1.7 - if that helps.
    – revive
    Commented Apr 23, 2010 at 3:44

3 Answers 3

3

You want an IF-statement, not a switch statement

2

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
}
4
  • 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
    Commented Apr 23, 2010 at 4:04
  • @revive,u're welcome.I would be more happier if you can accept the answer:-)
    – Young
    Commented Apr 23, 2010 at 5:42
  • 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
    Commented Apr 23, 2010 at 6:34
  • @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.
    – Young
    Commented Apr 23, 2010 at 6:54
0

Seems it could be simpler:

if(ereg("google", $link)){
    $class = "google";
}else if(ereg("yahoo", $link)){
    $class = "yahoo";
}else{
    $class = "";
}
1

Not the answer you're looking for? Browse other questions tagged or ask your own question.