Link to home
Create AccountLog in
Avatar of interclubs
interclubs

asked on

Help with Regex to Catch <br> tags at beginning AND end of line

I'm using the following regex to remove leading <br> <br /> tags and whitespace, and it works fine:
    $text = '<br><br>hi there <br><br>';
    echo preg_replace('/^(?:<br\s*\/?>\s*)+/', '', $text);

Open in new window


How can I modify it to also remove trailing <br> <br /> tags and whitespace though?

Thanks!
Avatar of nickinthooz
nickinthooz
Flag of United States of America image

$string = preg_replace('~^<br>$~', '', $string);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
yeah sorry, misread the question

The + tells it to keep going and remove all of them.