I have an array containing some words and I want to remove the words that contain either a . (fullstop) or ; (semicolon) or some other symbols. I have read the solution on [ Remove item from array if item value contains searched string character ] but this doesn't seem to answer my problem.
What can I add to this code to remove also the words containing the other symbols other than semicolon?
function myFilter($string) {
return strpos($string, ';') === false;
}
$newArray = array_filter($array, 'myFilter');
Thanks
Use preg_match
function:
function myFilter($string) {
return !preg_match("/[,.]/", $string);
}
[,.]
- character class which can be extended with any other symbols
// $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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.