1213
1213

Reputation: 756

Replace by nothing and remove the line

In a file, I replace a marker !!marker!! by a string or nothing:

if ($foo){
        insert_content('index.html', 'index.html', '!!marker!!', $string);
    }else{
        insert_content('index.html', 'index.html', '!!marker!!', '');
    }

…but as !!marker!! was on its own blank line, this line remains, and blank.

Is it possible to replace the marker by nothing and remove the line it was on?

Upvotes: 1

Views: 171

Answers (1)

vks
vks

Reputation: 67978

[^\n]*!!marker!![^\n]*\n

This should remove the line .Replace by empty string.See demo.

https://regex101.com/r/sJ9gM7/112

$re = "/[^\\n]*!!marker!![^\\n]*\\n/im";
$str = "asdasdasd\nsadasd !!marker!!a asdasd\nklklkl\na\nsd\nas\nd\n";
$subst = "";

$result = preg_replace($re, $subst, $str);

Upvotes: 2

Related Questions