I have following php code that removes whole word that matches the pattern
$patterns = ["re", "get", "ER"];
$string = "You are definitely getting better today";
$alternations = implode('|', $patterns);
$re = '(?!(?<=\s)(?:'.$alternations.')(?=\s))\S*(?:'.$alternations.')\S*';
$string = preg_replace('#'.$re.'#', '', $string);
$string = preg_replace('#\h{2,}#', ' ', $string);
echo $string;
I want two modifications
- The pattern search should not be case sensitive e.g. the pattern ER must remove better in $string
- If removed word in $string have line breaks before or after it, only one line break should be removed.
If $string is
You are definitely getting
better
today
Output must be
You definitely
today
Regards,