PHP Regex Remove leading and trailing <br /> within <p> tags -


i can't figure out life of me.

i need regex expression strip out leading or trailing <br> tags within <p> tags.

for example.

<p> <br />some test text. <br /> test text. test text. test text. test text. test text. test text. test text. test text. test text. test text. test text. <br /><br /><br /> </p>

should become...

<p>some test text. <br /> test text. test text. test text. test text. test text. test text. test text. test text. test text. test text. test text.</p>

i feel though should simple i've hit road-block. appreciated.

you need look-ahead , look-behind <p> , </p>, , non-capturing group 1 or more occurrences of variation of <br>.

for matching leading <br/> tags:

(?<=<p>)(?:\s*<br\s*\/?>)+\s* 

for matching trailing <br/> tags:

(?:\s*<br\s*\/?>)+\s*(?=<\/p>) 

both together:

(?<=<p>)(?:\s*<br\s*\/?>)+\s*|(?:\s*<br\s*\/?>)+\s*(?=<\/p>) 

regex101 demo


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -