0

I already have a code:

  <?
           $eventname = $event->title;
           $bad_words = array('Example1','Example2','Example3','Example4','Example5','Example6');

           foreach($bad_words as $bad_word){
               if(strpos($eventname, $bad_word) !== false) {
                 echo '';
                 break;
               }
               else {
                 echo '<div class="uk-text-contrast">'.translate('HIDDEN_INFO_DATE').'</div>';
                 break;
               }
           }
           ?>

this works, but only if the $eventname contains example1. But I want to hide echo '<div class="uk-text-contrast">'.translate('HIDDEN_INFO_DATE').'</div>'; from all of the defined words in the $bad_words.

How can I hide the echo if there is one of the $bad_words?

  • might be worth a ganders - https://stackoverflow.com/questions/6284553/using-an-array-as-needles-in-strpos – treyBake Apr 04 '18 at 12:49

4 Answers4

0

Why don't you use the in_array function? It's even shorter in that way.

if (in_array($eventname, $bad_words)) {
    echo '';
} else {
    echo '<div>';
}
Milkmannetje
  • 1,152
  • 1
  • 10
  • 35
0

remobe 'break' from code

foreach($bad_words as $bad_word){
               if(strpos($eventname, $bad_word) !== false) {
                 echo '';
               }
               else {
                 echo '<div class="uk-text-contrast">'.translate('HIDDEN_INFO_DATE').'</div>';
               }
           }
romal tandel
  • 481
  • 12
  • 19
0

Rather than using a loop, you could split the title into it's separate words and then check if there are any words in the $bad_words list using array_intersect()

// $eventname = $event->title;
$eventname = 'Some text';
$eventname = 'Some text Example1';
$bad_words = array('Example1','Example2','Example3','Example4','Example5','Example6');

$eventWords = explode(" ", $eventname);
if ( empty (array_intersect($eventWords, $bad_words)))  {
    // Event name is OK
    echo $eventname;
}

This has some examples to show how it works.

Note that this code doesn't pick up things like Example1234 which strpos() would.

Update:

Just to make it more flexible, you can use regular expressions in case you have punctuation in the title...

$eventname = 'Some text';
$eventname = 'Some text,Example1';
$bad_words = array('Example1','Example2','Example3','Example4','Example5','Example6');

preg_match_all('/(\w+)/', $eventname, $eventWords);

if ( empty (array_intersect($eventWords[1], $bad_words)))  {
    echo '<div class="uk-text-contrast">'.translate('HIDDEN_INFO_DATE').'</div>';
}

( Note: Regular expressions aren't my subject so please let me know if this needs to be improved).

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
0

I would convert the string $eventname to an array and then use the array Intersect function

    $eventname = "sgswfdg Example1 sdfh dh  dfa hadhadh";   
$title_to_array=explode(" ", $eventname);
$bad_words = array('Example1','Example2','Example3','Example4','Example5','Example6');

$result = !empty(array_intersect($bad_words , $title_to_array ));

if ($result){
    echo '';
    } else {
    echo $eventname;
}