tags:

views:

470

answers:

6

guys i have a text file and i want to remove some lines that contain specific words

 <?php
// set source file name and path
$source = "problem.txt";

// read raw text as array
$raw = file($source) or die("Cannot read file");

now there's array from which i want to remove some lines and want to use them so on

example words, martin , methew or like: martin is asshole

thanks

+2  A: 

Check out the strpos function. It can tell you if a string contains another string or not (and where exactly the first string is in the second). You would use it like this:

$good = array();
$bad_words = array('martin', 'methew');

// for every line in the file
foreach($raw as $line) {
  // check for each word we want to avoid
  foreach($bad_words as $word) {
    // if this line has a trigger word
    if(strpos($line, $word) !== false) {
      // skip it and start processing the next
      continue 2;
    }
  }

  // no triggers hit, line is clean
  $good[] = $line;
}

Now you would have a list of only clean lines in $good.

Cryo
just tell me wat to do if i want to make a another badwords.txt file and use it with this solution, i tried but failed, acctually i dont want to open the code again n again :D thanks for help
Jimmy
A: 
<?php
$source = "problem.txt";
$raw = file_get_contents($source) or die("Cannot read file");
$wordlist = "martin|methew|asshole";
$raw = preg_replace("/($wordlist)/ie", "", $raw);
file_put_contents($source, $raw);
?>
fire
wow amazing help quicklyy and now i too much happy and confused :Dwell i m sure something gona work, thanks alot guys god bless you
Jimmy
+5  A: 

As you have each line of your file in a row of an array, the array_filter function might interest you (quoting) :

array array_filter  ( array $input  [, callback $callback  ] )

Iterates over each value in the input array passing them to the callback function.
If the callback function returns true, the current value from input is returned into the result array. Array keys are preserved.

And you can use strpos or stripos to determine if a string is contained in another one.


For instance, let's suppose we have this array :

$arr = array(
  'this is a test',
  'glop test',
  'i like php',
  'a badword, glop is', 
);

We could define a callback function that would filter out lines containing "glop" :

function keep_no_glop($line) {
  if (strpos($line, 'glop') !== false) {
    return false;
  }
  return true;
}

And use that function with array_filter :

$arr_filtered = array_filter($arr, 'keep_no_glop');
var_dump($arr_filtered);

And we'd get this kind of output :

array
  0 => string 'this is a test' (length=14)
  2 => string 'i like php' (length=10)

i.e. we have removed all the lines containing the "badword" "glop".


Of course, now that you have the basic idea, nothing prevents you from using a more complex callback function ;-)


Edit after comments : here's a full portion of code that should work :

First of all, you have your list of lines :

$arr = array(
  'this is a test',
  'glop test',
  'i like php',
  'a badword, glop is', 
);

Then, you load the list of bad words from a file :
And you trim each line, and remove empty lines, to make sure you only end up with "words" in the $bad_words array, and not blank stuff that would cause troubles.

$bad_words = array_filter(array_map('trim', file('your_file_with_bad_words.txt')));
var_dump($bad_words);

The $bad_words array contains, from my test file :

array
  0 => string 'glop' (length=4)
  1 => string 'test' (length=4)

Then, the callback function, that loops over that array of bad words :
Note : using a global variable is not that nice :-( But the callback function called by array_filter doesn't get any other parameter, and I didn't want to load the file each time the callback function is called.

function keep_no_glop($line) {
  global $bad_words;
  foreach ($bad_words as $bad_word) {
      if (strpos($line, $bad_word) !== false) {
        return false;
      }
  }
  return true;
}

And, as before, you can use array_filter to filter the lines :

$arr_filtered = array_filter($arr, 'keep_no_glop');
var_dump($arr_filtered);

Which, this time, gives you :

array
  2 => string 'i like php' (length=10)

Hope this helps.

Pascal MARTIN
thanks alot for explaining the whole
Jimmy
You're welcome :-) Have fun !
Pascal MARTIN
just tell me wat to do if i want to add a list of words, like if i can write a another list of words,one word per line then ? and if i want to add more words with glop
Jimmy
A solution would be to use `strpos` for each word or your "bad words" list ; i.e. using `foreach` to loop over the array, testing each word against the line, with `strpos` ;;; or if you want a more basic comparison, using `preg_match` could be OK too, using a regex such as `/word1|word2|word3/`
Pascal MARTIN
acctually i have changed my mind and now i want to make another text file contain bad words/lines at each line which shud be matched and removed accordingly any idea ?
Jimmy
The easier way would, then, be to load the file into an array, and use that array like I said in my previous comment *(i.e. the function that checks whether a line is OK or not doesn't need to know where the list of words comes from)* ;;; and you already know how to load a file into an array ;-)
Pascal MARTIN
lol i m not a programmer just trying my luck with some code :D , i m learning it quickly :D
Jimmy
Learning is great ;-) -- and we all started not knowing much about programming ^^ *(And happilly, we are still learning, even after a couple of years)*
Pascal MARTIN
now i m using 2nd answer plz just tell me how to load a file with that
Jimmy
I've edited my answer to provide a more complete example, that load the list of bad words from a file ; hope this helps.
Pascal MARTIN
gona check it, thanks
Jimmy
wow it works and i learned to much today, books sucks sometimes and practical rocks :D
Jimmy
Practice (with trials and errors, of course) is the best way to learn, in my opinion :-)
Pascal MARTIN
+1  A: 

This will remove all rows that have a blacklisted word in it:

$rows = file("problem.txt");    
$blacklist = "foo|bar|lol";

foreach($rows as $key => $row) {
    if(preg_match("/($blacklist)/", $row)) {
        unset($rows[$key]);
    }
}

file_put_contents("solved.txt", implode("\n", $rows));

Or, if you are using PHP 5.3, you can use a lambda function with array_filter:

$rows = file("problem.txt");    
$blacklist = "foo|bar|lol";
$rows = array_filter($rows, function($row) {
    return preg_match("/($blacklist)/", $row);
});

file_put_contents("solved.txt", implode("\n", $rows));

Prior to PHP 5.3, a solution using array_filter would actually use up more rows than the first solution I posted, so I'll leave that out.

Tatu Ulmanen
A: 

Assuming that you have an array of "bad words":

<?php
foreach ($raw as $key=>$line)
{
    foreach ($badwords as $w)
    {
        if ( strpos($line, $w) !== false )
            unset($raw[$key]);
    }
}
?>
DisgruntledGoat
A: 
$file=file("problem.txt");
$a = preg_grep("/martin|john/",$file,PREG_GREP_INVERT );
print_r($a);
ghostdog74