How to remove markup tags outside of a paragraph

I want to remove all tags <br>

outside <p></p>

. But interruptions inside <p></p>

shouldn't suffer. 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 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


source to share


3 answers


This will help you.



$out = preg_replace("(<p(?:\s+\w+(?:=\w+|\"[^\"]+\"|'[^']+')?)*>.*?</p>(*SKIP)(*FAIL)"
."|<br>)is", "", $html);

echo $out;

      

+4


source


<?php
$text = '<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>';

$pattern = '/(<br>[\s\r\n]*<p>|<\/p>[\s\r\n]*<br>)/';
$replacewith = '<p>';
echo preg_replace($pattern, $replacewith, $text);

      



+1


source


below is this solution, maybe it can help you.

$html = "";
$html .="<p><br>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>";

        $html .= "<p><br> 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>";

echo $html;

      

0


source







All Articles