Expression
First, let it work out an expression that you will need to match with an array of words:
/(?:word1|word2|word3)/
An expression (?: ... ) creates a group without writing its contents to a memory location. Words are separated by a pipe symbol, so that it matches any word.
To generate this expression using PHP, you will need the following construct:
$unwanted_words = array("word1", "word2", "word3"); $unwanted_words_match = '(?:' . join('|', array_map(function($word) { return preg_quote($word, '/'); }, $unwanted_words)) . ')';
You need preg_quote() to create the correct regular expression from a regular string if you are not sure if it is valid, for example. "abc" does not need to be specified.
See also: array_map() preg_quote()
Using an array of strings
You can break a block of text into an array of strings:
$lines = preg_split('/\r?\n/', $block_of_lines);
Then you can use preg_grep() to filter strings that don't match and create another array:
$wanted_lines = preg_grep("/$unwanted_words_match/", $lines, PREG_GREP_INVERT);
See also: preg_split() preg_grep()
Using a single preg_replace()
To match an entire line containing an unwanted word inside a block of text with multiple lines, you need to use line bindings, for example:
/^.*(?:word1|word2|word3).*$/m
Using the /m modifier, the ^ and $ bindings correspond to the beginning and end of the line, respectively. .* on both sides "flushes" the expression to the left and to the right of the matching word.
It should be noted that $ matches immediately before the actual line termination character (either \r\n or \n ). If you perform a replacement using the above expression, it will not replace the strings themselves.
You need to match these extra characters by expanding the expression as follows:
/^.*(?:word1|word2|word3).*$(?:\r\n|\n)?/m
Did I add (?:\r\n|\n)? anchor $ to match the optional string. This is the last code to replace:
$replace_match = '/^.*' . $unwanted_words_match . '.*$(?:\r\n|\n)?/m'; $result = preg_replace($replace_match, '', $block_of_lines);
Demo