Home > Software engineering >  Remove <br /> tags which are unwanted/invalid children of a <ul> tag
Remove <br /> tags which are unwanted/invalid children of a <ul> tag

Time:08-27

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.

CodePudding user response:

Use preg_replace_callback() to replace everything between <ul> and </ul>, using a callback function that removes <br />

$text = preg_replace_callback('/<ul>.*?</ul>/s', function($matches) {
    return str_replace('<br />', '', $matches[0]);
}, $text);

CodePudding user response:

You should not use regex to parse HTML because it doesn't know the difference between tags and text that looks like tags.

It may look like more code, but it is code that will be more reliable, easier to read, and easier to maintain.

The XPath query says: "target all br tags that are the children of ul and the ul tags can be found in any depth of the document.

Code: (Demo)

$html = <<<HTML
<div>
<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>
</div>
HTML;

libxml_use_internal_errors(true);
$dom = new DOMDocument; 
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($dom);

foreach ($xpath->query('//ul/br') as $br) {
    $br->parentNode->removeChild($br);
}
echo $dom->saveHTML();

Output:

<div>
<p>Paragraph 1 <br> Break line</p>
     <ul> 
    <li>  New Line</li>   
    <li> Second line </li>   
    <li> Third line </li>   
    </ul>  
<p>Paragraph two. <br> break line</p>
</div>
  • Related