hi

(i double posted this in the wrong forum earlier, sorry about that)

i'm new to php, not sure if anyone can help but it would be greatly appreciated..... i want to load 2 files into arrays (that works) and then use array_diff to create a new array like this:

$words = file ('./words.txt');
$bannedwords = file ('./bannedwords.txt');
$words2 = array_diff ($words, $bannedwords);

but the banned words need to be wildcards e.g. if

$words = ("hello to you", "goodbye to you", "phrase with swearword");
$bannedwords = ("badword", "swearword");

the output produced would be:

$words2 = ("hello to you", "goodbye to you");

i.e. "swearword" would become "%swearword%" in the $bannedwords array.

i am not sure how to do that, can anyone help? Thanks in advance.

roland

    check out this 2 function in_array() array_search()
    goto php.net for more information

      you have a complex problem and i dont believe there is a built-in function that does it for you..

      i guess you will need a looping

      $output = $dirty_j = array();
      for ($i=0; $i<count($badwords); $i++) {
        for ($j=0; $j<count(texts); $j++) {
          if (in_array($j, $dirty_j)) continue; // has some bad word
      
      if (strstr($texts[$j], $badwords[$i])) {
        echo 'bad word '.$badwords[$i].' found at '.$texts[$j];
        $dirty_j[] = $j;
        continue; // to next text;
      } else {
        $output[] = $texts[$j];
      }
        }
      }
      
      print_r($output);
      

      []

        The best way I can think of is to use array_filter, where your filter function would take each phrase passed to it and see if it contained a word from the swearword array, and return 'false' if it does.

          thanks guys for all your help, it's really appreciated 🙂 i'll go through those ideas and see how i get on, and as a newbie, i'll no doubt learn something along the way.

          thanks again

          roland

            Write a Reply...