I just answered a question here, which has left me asking my own question.
Basically, the OP wanted any line to be removed if it contained a string. Here is a regex I came up with…
$str = preg_replace('/^.*\b["(]?hello["(]?\b.*$/m', '', $str);
It works great, but because $ matches before the trailing \n, they remain when replaced and there are blank lines, e.g.
string(90) "
What it shouldhello do is:
could be words in between brackets and inverted commas also."
I can’t use \z because I’m in multiline mode (at least that is what I think).
If I use s modifier, the . become too greedy and don’t work across the newlines.
I have tried a few things (such as [^\n] and [\s\S]), and now I am stumped.
How can I match that trailing \n here so it is removed with the replace?
Use \n instead of $.
This misses the last line if it had hello in it, so here’s a step further.
The issue now is that the last line is removed, but there is still a \n character (similar issue you’re already having).
Note: I’m not a regular expression expert at all, just can usually do enough for my needs.