Reputation: 642
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>"
Upvotes: 3
Views: 180
Reputation: 1
below is solution may be 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;
Upvotes: 0
Reputation: 402
This will help you.
$out = preg_replace("(<p(?:\s+\w+(?:=\w+|\"[^\"]+\"|'[^']+')?)*>.*?</p>(*SKIP)(*FAIL)"
."|<br>)is", "", $html);
echo $out;
Upvotes: 4
Reputation: 1052
<?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);
Upvotes: 1