1

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

1
  • Can we see the array your using. Commented Jan 9, 2017 at 22:09

2 Answers 2

2

Use preg_match function:

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

[,.] - character class which can be extended with any other symbols

Sign up to request clarification or add additional context in comments.

1 Comment

Nice and clean solution. Thanks
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.