Php preg_replace removes the entire line (from a block of many lines) if it contains an occurrence of the word

Guys (preg_replace gurus);

I am looking for a preg_replace fragment that I can use in a php file in which, if a word appears on a specific line, this whole line is deleted / replaced with an empty line

pseudo code:

$unwanted_lines=array("word1","word2"."word3"); $new_block_of_lines=preg_replace($unwanted_lines, block_of_lines); 

Thanx, Marco

+7
php regex preg-replace
source share
3 answers

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

+7
source share

This regex can remove a match from a string

 $newstring = preg_replace("/^.*word1.*$/", "", $string); 
+1
source share

As @jack pointed out, let's just use preg_quote() && array_map()

 $array = array('word1', 'word2', 'word3', 'word#4', 'word|4'); $text = 'This is some random data1 This is some word1 random data2 This is some word2 random data3 This is some random data4 This is some word#4 random data5 This is some word|4 random data6 This is some word3 random data7'; // Some data $array = array_map(function($v){ return preg_quote($v, '#'); }, $array); // Escape it $regex = '#^.*('. implode('|', $array) .').*$#m'; // construct our regex $output = preg_replace($regex, '', $text); // remove lines echo $output; // output 

Demo version

+1
source share

All Articles