hi

i'm a php newbie, 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? Thnaks in advance.

roland

    <?php
    
    $words = file ('./words.txt');
    $bannedwords = file ('./bannedwords.txt');
    $words2 = '';
    settype($words2,"array");
    
    
    for($i=0;$i<sizeof($words);$i++) {
       for($j=0;$j<sizeof($bannedwords);$j++) {
          $bw = $bannedwords[$j];
          if(!preg_match("/$bw/",$words[$i]) ) {
             $words2[] = $words[$i];
          }
    }
    
    // your output...
    ?>
    

    Now, I hate to use nested loops because the execution time will increase exponentially as the number of items increases...

    But anyway, there's an idea 🙂

      thanks tbach, that looks good for my needs, i'll give it a try.

      thanks again.

      Roland

        Write a Reply...