Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove <p><strong><br /> &nbsp;</strong></p> with XPATH

I use xpath to remove <p>&nbsp;</p>

    $nodeList = $xpath->query("//p[text()=\"\xC2\xA0\"]"); # &nbsp;
    foreach($nodeList as $node) 
    {
        $node->parentNode->removeChild($node);
    }

but it does not remove this,

<p><strong><br /> &nbsp;</strong></p>

or this kind,

<p><strong>&nbsp;</strong></p>

How can I remove them?

Or maybe a regex that I should use?

like image 430
Run Avatar asked Oct 21 '11 23:10

Run


People also ask

How to remove the HTML tags in PHP?

The strip_tags() function strips a string from HTML, XML, and PHP tags. Note: HTML comments are always stripped. This cannot be changed with the allow parameter. Note: This function is binary-safe.

How do you remove a BR tag from a string?

The line break can be removed from string by using str_replace() function.

How do I remove text tags in HTML?

The HTML tags can be removed from a given string by using replaceAll() method of String class. We can remove the HTML tags from a given string by using a regular expression. After removing the HTML tags from a string, it will return a string as normal text.

How remove all HTML tags from a string in laravel?

you can use strip_tags($string) to strip the html tags.


1 Answers

Try with

$nodeList = $xpath->query("//p[normalize-space(.)=\"\xC2\xA0\"]"); # &nbsp;
foreach($nodeList as $node) 
{
    $node->parentNode->removeChild($node);
}

Quoting from the docs

The normalize-space function returns the argument string with whitespace normalized by stripping leading and trailing whitespace and replacing sequences of whitespace characters by a single space.

like image 87
Gabriele Petrioli Avatar answered Oct 15 '22 15:10

Gabriele Petrioli