'Delete the line contains specific words/phrases with PHP
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.
Solution 1:[1]
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.
Solution 2:[2]
$file=file("problem.txt");
$a = preg_grep("/martin|john/",$file,PREG_GREP_INVERT );
print_r($a);
Solution 3:[3]
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.
Solution 4:[4]
If you have a long string rather than a file and you want to remove all string lines that has specific words. You can use this:
$string="I have a long string\n
That has good words inside.\n
I love my string.\n
//add some words here\n";
$rows = explode("\n",$string);
$unwanted = "tring|\/\/";
$cleanArray= preg_grep("/$unwanted/i",$rows,PREG_GREP_INVERT);
$cleanString=implode("\n",$cleanArray);
print_r ( $cleanString );
This removes that has lines contain "tring" and "//".
Solution 5:[5]
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]);
}
}
?>
Solution 6:[6]
<?php
$source = "problem.txt";
$raw = file_get_contents($source) or die("Cannot read file");
$wordlist = "martin|methew";
$raw = preg_replace("/($wordlist)/i", "", $raw);
file_put_contents($source, $raw);
?>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tatu Ulmanen |
| Solution 2 | ghostdog74 |
| Solution 3 | |
| Solution 4 | |
| Solution 5 | DisgruntledGoat |
| Solution 6 | mickmackusa |
