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


Change string if include strings from an array

我创建了这个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;
}
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;
}
$yourinputvalue = "whatever";    
$something = array (''.CS', ' Dota2', '-Dota2', ' Esports', 'eSports', ' Tactics', 'DotCom', ' DotA2', ' - LoL', '-LoL', ' Carry', ' LoL', ':)', '.de'');
    if (!in_array($yourinputvalue, $something))
    {
        //do whatever
    }

您可以使用array_search函数,例如:

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