2017-01-09 190 views
1

我有一個包含某些單詞的數組,並且我想要刪除包含某個單詞的單詞。 (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

回答

2

使用preg_match功能:

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

[,.] - 使用數組你的性格類可以與任何其他符號

+0

乾淨整潔的解決方案。謝謝 –

+0

@ L.D,不客氣 – RomanPerekhrest

1
// $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); 
相關問題