How can I remove these <br />
tags from between the <ul>
tags?
<p>Paragraph 1 <br /> Break line</p>
<ul> <br />
<li> New Line</li> <br />
<li> Second line </li> <br />
<li> Third line </li> <br />
</ul>
<p>Paragraph two. <br /> break line</p>
The <br />
tags between list items are adding lines between list items.
How can I remove these <br />
tags in between <ul> </ul>
only? I don't want any other <br/>
tags to be removed.
My Answer:
You can use PHP's DOMDocument class to remove the unwanted `
` tags between `
- ` and `
php
$html = 'Paragraph 1
Break line
- New Line
- Second line
- Third line
Paragraph two.
break line
';
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$brNodes = $xpath->query('//ul/br');
foreach ($brNodes as $brNode) {
$brNode->parentNode->removeChild($brNode);
}
$cleanHtml = $doc->saveHTML();
echo $cleanHtml;
This code will remove all `
` tags that are direct children of `
- ` tags in the provided HTML string.
Rate this post
4 of 5 based on 1169 votesComments