开发者

Help with a simple switch statement

开发者 https://www.devze.com 2022-12-28 02:05 出处:网络
I need to find the value of a variable and use it to add a class to a div, based on a switch statement.

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..开发者_JAVA技巧.
            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


You want an IF-statement, not a switch statement


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
}


Seems it could be simpler:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号