I want to replace <br> with all its surrounding whitespaces with a linebreak \n and two whitespaces .
Here is an example of what I tried using regex:
preg_replace('#\s*<br>\s*#', "\n  ", "Der\n <br>      Hund");
I was expecting this:
But instead, I got this:
Which shows that <br> was replaced by two whitespaces, but not a line-break.
If I change the regex so that it does not match the whitespaces before the <br> it magically works: 
preg_replace('#<br>\s*#', "\n  ", "Der\n <br>      Hund");
and I get as expected:
Why is the first regex not working as expected? I am using PHP 7.2.24.



 
    
`? `'#\h*
\h*#'` / `'#\h*
\h*#u'`? – Wiktor Stribiżew May 10 '20 at 11:54