duanhui3759
2014-12-24 19:55
浏览 79
已采纳

使用带有haystack的strpos作为数组(在WordPress中)?

Is there a way to use strpos() with the $haystack converted from an array to a string? My temporary solution was to use in_array() instead, but it is less than ideal:

function dynamic_id() {

    $find_str = get_body_class();

    if ( in_array( 'single-sfwd-courses', $find_str ) || in_array( 'single-sfwd-lessons', $find_str ) || in_array( 'single-sfwd-topic', $find_str) || in_array( 'single-sfwd-quiz', $find_str ) ) {
        echo 'id="jumbo_bg1"';
        echo '><style type="text/css">
        .jumbotron a.btn {
            display: none;
        }
        </style';
    } else {
        echo 'id="jumbo_bg"';
    }
}

Any help would be much appreciated. Thanks!

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doushi4633 2014-12-24 20:20
    已采纳

    It looks like you are trying to see if any of those values in in your array. In that case, I'd try to make an array of values then use array_intersect() to see if any of them match.

    If array_intersect() returns an array with at least one value, that means there was a match.

    function dynamic_id() {
        $find_str = get_body_class();
        $vals = array('single-sfwd-courses', 'single-sfwd-lessons', 'single-sfwd-topic', 'single-sfwd-quiz');
    
        $intersect = array_intersect($find_str, $vals);
    
        if(count($intersect) > 0){
            // There was a match
        }
        else{
            // No match
        }
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题