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)
, .