Page 1 of 1

Help on checking blacklisted words.

Posted: 08 Jun 2011 14:39
by tiago
Below code is very raw and it was done after HOURS trying to get the desired effect so expect it to be a(nother) flaw. Previous code I was using was buggy/inaccurate so I'm asking community to help me on this.
Please, I need to know how to check a string against a bunch of forbidden, blacklisted words. If the word is not part of the list, continue. Otherwise, pick another word and test it again, assigning it as valid.
So many things formatlist() could do for us... ;)

Code: Select all

...
   $ep = ",";
   $p_nm1 = "Name,de,Name2";
...
   $mn1 = "de";

   $indx = 1;
   $frbd = " ,da,das,de,do,dos,of,the";

        foreach($tkn, $frbd, "$ep")
                                            {
   $flag = 1;
   WHILE ($flag != 0) {
   IF ($tkn == $mn1) { $flag = 1; $mn1 = gettoken("$p_nm1", "$indx", "$ep"); $indx++; }
   ELSEIF ($tkn != $mn1) { $flag = 0; }
                      }
                                            }
...

Re: Help on checking blacklisted words.

Posted: 11 Jun 2011 04:03
by tiago
Any help on this, please...?

Re: Help on checking blacklisted words.

Posted: 11 Jun 2011 12:08
by Stefan
Please see if this can help you:

Code: Select all

//Test 1: nothing from blacklist is found, so all checks are negative and the script will always continue.
  $blacklist = "Seat,Fiat,Skoda";
  $input = "My new car is an AUDI";

  foreach($tkn, $blacklist, ",")
  {
    if ($tkn==""){break;}
   
    if strpos($input,$tkn) == -1 )
    {
         msg "Test 1<crlf 2>Continue.<crlf>$tkn was NOT found in<crlf>$input.";
    }else{
         msg "Test 1<crlf 2>Skipped.<crlf>$tkn WAS found in<crlf>$input.";
    }
  }

Code: Select all

////////////////////////////////////////////////////////////////////////////////////
//Test 2: One from blacklist is found, so all checks but the last are negative, and the script will skip once.
  $blacklist = "Seat,Fiat,Skoda";
  $input = "My new car is an Skoda";

  foreach($tkn, $blacklist, ",")
  {
    if ($tkn==""){break;}
   
    if strpos($input,$tkn) == -1 )
    {
         msg "Test 2<crlf 2>Continue.<crlf>$tkn was NOT found in<crlf>$input.";
    }else{
         msg "Test 2<crlf 2>Skipped.<crlf>$tkn WAS found in<crlf>$input.";
    }
  }

Code: Select all

////////////////////////////////////////////////////////////////////////////////////
//Test 3: how-to check case in-sensitive.
  $blacklist = "Seat,Fiat,Skoda";
  $input = "My new car is an SKODA";

  foreach($tkn, $blacklist, ",")
  {
    if ($tkn==""){break;}
   
    //not case-sensitive:
    $tkn = recase($tkn, "lower");
    $input = recase($input, "lower");
    if strpos($input,$tkn) == -1 )
    {
         msg "Test 3<crlf 2>Continue.<crlf>$tkn was NOT found in<crlf>$input.";
    }else{
         msg "Test 3<crlf 2>Skipped.<crlf>$tkn WAS found in<crlf>$input.";
    }
  }

Code: Select all

////////////////////////////////////////////////////////////////////////////////////
//Test 4: more practically use by separating function from result. 
  $blacklist = "Seat,Fiat,Skoda";
  $input = "My new car is an AUDI";

  foreach($tkn, $blacklist, ","){   if ($tkn==""){break;}
    $tkn = recase($tkn, "lower");   $input = recase($input, "lower");
    if strpos($input,$tkn) == -1 )  {$found = false;}else{$found = true; break;} 
    //(Note that we break; the loop once we are True) 
  }

  if ( ! $found)
  {
    msg "Test 4<crlf 2>not found";
  }
    else
  {
    msg "Test 4<crlf 2>found";
  }

Code: Select all

////////////////////////////////////////////////////////////////////////////////////
//Test 5: like Test4 but as an separate function:
  global $blacklist, $input, $FuncResult;
  $blacklist = "Seat,Fiat,Skoda";
  $input = "My new car is an FIAT";

  sub "_function"; 

  if ( ! $FuncResult){ 
     msg "Test 5<crlf 2>not found";
  }else{
     msg "Test 5<crlf 2>found";
  }
  
//////////////////////////
"_function"
  global $blacklist, $input, $FuncResult;
  foreach($tkn, $blacklist, ","){   if ($tkn==""){break;}
    $tkn = recase($tkn, "lower");   $input = recase($input, "lower");
    if strpos($input,$tkn) == -1 )  {$FuncResult = false;}else{$FuncResult = true; break;}
  }

Re: Help on checking blacklisted words.

Posted: 11 Jun 2011 19:16
by tiago
As my script is too complex* I can't fully test how your code will fit it for now, Stefan, but first attempts on the issue itself appear that everything will be ok.
So I thank you for this.

*this problem was caught on a revision of a 5th bug found on it so you may have a picture. 8)

Re: Help on checking blacklisted words.

Posted: 11 Jun 2011 22:06
by tiago
I made a few adjustments so the script finally deals well with null/blank/non-existant characters both on initial input and of course in the blacklist. I think this is it Stefan so thank you very much.

Re: Help on checking blacklisted words.

Posted: 12 Jun 2011 03:51
by Wanda
how could i use this please?

Re: Help on checking blacklisted words.

Posted: 13 Jun 2011 23:42
by tiago
Wanda wrote:how could i use this please?
This is a generic piece of script which is being used on a greater script so I'm afraid you won't have much use for this.
If you thought on a specific use for this drop us a note and we may be able to help u.