So i ran a special dir command on about 2000 workstations. i then piped that out to a text file.

they look like
C:\blah\1234.txt
C:\windows\test.txt
C:\temp\blah.txt
etc etc etc

Well i got a lot of false positives, so i want to ignore lines that contain certain file names, and echo the rest.

So i created this

<?php


$__BADWORDSARRAY = array ("test.txt", "blah.txt");


function containsBadword($line) {
  global $__BADWORDSARRAY;
  for ($i = 0; $i < count($__BADWORDSARRAY); $i++) {
    if (stristr($__BADWORDSARRAY[$i],$line)) {
      return(true);
     }
  }
  return(false);
}



$lines =  file("c:\\test.txt");

foreach($lines as $linenum => $line){

if (!containsBadword($line)) {
  echo $line . " - no bad words\r\n";
} else {
  echo "omgwtfbbq!! potty mouth<br>";
}


}

?>

and my output is

c:\test.txt
 - no bad words
blah.txt
 - no bad words
test\blah.txt\test
 - no bad words
testing
 - no bad words
1234 - no bad words

so why isn't it finding text.txt and blah.txt

    <?php
        $filestring = file_get_contents("C:\\test.txt");
        $filearray = explode("\n", $filestring);
    $__BADWORDSARRAY = array ("test", "blah");
    
    
    function containsBadword($line) {
      global $__BADWORDSARRAY;
      for ($i = 0; $i < count($__BADWORDSARRAY); $i++) {
        if (stristr($line,$__BADWORDSARRAY[$i])) {
          return(true);
         }
      }
      return(false);
    }
        while (list($var, $val) = each($filearray)) {
            ++$var;
            $val = trim($val);
            //print "Line $var: $val<br />";
    
    	if (!containsBadword($val)) {
      echo $val . " - no bad words\r\n";
    } else {
      echo "omgwtfbbq!! potty mouth<br>";
    }
    
    }
    ?> 
    

    seems to work instead

      As you've found, your original code had the arguments to [man]stristr[/man] backwards.

      See also [man]preg_grep[/man].

        Write a Reply...