Delete line contains specific words / phrases using PHP

Guys, I have a text file and I want to delete some lines containing certain 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 is an array from which I want to delete some rows and want to use them like this.

+5
source share
7 answers

Since you have every line of your file in an array line, the function array_filtermay interest you (quoting):

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

Iterate over each value in the input array, passing them into a function callback.
If the callback function returns true, the current value from the input is returned to the array of results. Array keys - saved.

strpos stripos, , .


, , :

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

, , "glop":

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

array_filter:

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

:

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

. , "badword" "glop".


, , , ; -)


: , :

, :

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

:
, , "" $bad_words, , .

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

$bad_words :

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

, :
: :-( , array_filter, , , .

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

, , array_filter :

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

, , :

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

, .

+7

strpos. , ( ). :

$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;
}

$good.

+2

:

$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));

, PHP 5.3, - 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));

PHP 5.3 array_filter , , , .

+2
$file=file("problem.txt");
$a = preg_grep("/martin|john/",$file,PREG_GREP_INVERT );
print_r($a);
+2

, , , . :

$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 it has lines containing "tring" and "//".

+2
source

Assuming 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]);
    }
}
?>
+1
source
<?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);
?>
0
source

All Articles