如果包含数组中的字符串,请更改字符串

时间:2015-03-18 16:34:11

标签: php jquery

我创建了这个jQuery方法,它检查输入是否包含数组中的一个字符串,如果包含,则删除它。我怎么能用PHP做到这一点?

function changeName(name) {
    var team = name,
    removearray = ['.CS', ' Dota2', '-Dota2', ' Esports', 'eSports', ' Tactics', 'DotCom', ' DotA2', ' - LoL', '-LoL', ' Carry', ' LoL', ':)', '.de'];

    removearray.forEach(function( word ) {
        team = team.replace(word, "");
    });

    if (team == "Copenhagen") {
        team = "CPH Wolves";
    } else if (team == "Counter") {
        team = "CLG";
    } else if (team == "Samsung Galaxy") {
        team = "Samsung Galaxy";
    } else if (team == "Meet Your") {
        team = "MYM";
    } else if (team == "Wheel Whreck While Whistling") {
        team = "Wheel";
    } 

    return team;
}

3 个答案:

答案 0 :(得分:0)

$yourinputvalue = "whatever";    
$something = array (''.CS', ' Dota2', '-Dota2', ' Esports', 'eSports', ' Tactics', 'DotCom', ' DotA2', ' - LoL', '-LoL', ' Carry', ' LoL', ':)', '.de'');
    if (!in_array($yourinputvalue, $something))
    {
        //do whatever
    }

答案 1 :(得分:0)

function changeName($name)
{
    $remove = ['.CS', ' Dota2', '-Dota2', ' Esports', 'eSports', ' Tactics', 'DotCom', ' DotA2', ' - LoL', '-LoL', ' Carry', ' LoL', ':)', '.de'];

    foreach ($remove as $r) {
        $name = str_replace($r, '', $name);
    }

    $replacements = array(
        'Copenhagen'                   => 'CPH Wolves',
        'Counter'                      => 'CLG',
        'Samsung Galaxy'              => 'Samsung Galaxy',
        'Meet Your'                    => 'MYM',
        'Wheel Whreck While Whistling' => 'Wheel',
    );

    $name = isset($replacements[$name]) ? $replacements[$name] : $name;

    return $name;
}

答案 2 :(得分:0)

您可以使用array_search功能,例如:

$arr = ['str1', ..., 'strN'];
$keyToUnset = array_search('something', $arr);
if ($keyToUnset !== false ) {
    $arr[$keyToUnset] = ''; 
}