如果包含一些字符串/符号,则从数组中删除元素

问题描述:

我有一个包含某些单词的数组,并且我想要删除包含某个单词的单词。 (fullstop)或; (分号)或其他符号。我已阅读[Remove item from array if item value contains searched string character]上的解决方案,但这似乎无法解决我的问题。如果包含一些字符串/符号,则从数组中删除元素

我可以在此代码中添加什么来删除包含除分号之外的其他符号的单词?

function myFilter($string) { 
    return strpos($string, ';') === false; 
} 

$newArray = array_filter($array, 'myFilter'); 

感谢

+0

扩展可我们看到的。 – Kitson88

使用preg_match功能:

function myFilter($string) { 
    return !preg_match("/[,.]/", $string); 
} 

[,.] - 使用数组你的性格类可以与任何其他符号

+0

干净整洁的解决方案。谢谢 –

+0

@ L.D,不客气 – RomanPerekhrest

// $array is your initial array 
$newArray = array(); 
foreach ($array as $item){ 
    if ((strpos($item, ';') > 0)||(strpos($item, '.') > 0)) 
     continue; 
    $newArray[] = $item; 
} 

// Words with ; or . should be filtered out in newArray 
print_r($newArray);