How to remove break tags from outside the paragraph

Viewed 162

I want to remove all the <br> tags outside the <p></p>. But the breaks inside the <p></p> should not be hurt. How can i achieve this in php. Below is an example.

$html = "<br> <p> This is the Firs para <br> 
        This is a line after the first break <br>
        This is the line after the 2nd break <br>
        Here the first para ends </p>
        <br>
        <br>
        <p> This is the 2nd para <br> 
        This is a line after the first break in 2nd para <br>
        This is the line after the 2nd break <br>
        Here the 2nd para ends </p>"

I want the result to be as below

$html = "<p> This is the Firs para <br> 
        This is a line after the first break <br>
        This is the line after the 2nd break <br>
        Here the first para ends </p>
        <p> This is the 2nd para <br> 
        This is a line after the first break in 2nd para <br>
        This is the line after the 2nd break <br>
        Here the 2nd para ends </p>"
3 Answers
Related