0

Newbie here. I'm trying to make it so if $value doesn't contain two dashes then it is unset from the array. I figured out how to remove items from the array containing only ONE dash, but it needs to contain TWO. Thanks!

Removes items from an array containing only one dash:

 foreach ($addkeys as $key=>&$value) {
 if (strpos($value, '-') === false) {
       unset($addkeys[$key]);
}
}

I also tried $value, '--' and '-''-' without success.

1 Answers1

0

You need to use preg_match_all(). Here is how it would look in your code. The function returns the count of items found:

<?php
$string = "/\-/";

foreach ($addkeys as $key => &$value) {
  if (preg_match_all($string, $value, &$matches)) {
    if (count($matches[0]) > 1)
      // greater than one.
  }
}
?>
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252