Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1062913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:42:14+00:00 2026-05-16T18:42:14+00:00

I have a script in PHP which removes empty paragraphs from an HTML file.

  • 0

I have a script in PHP which removes empty paragraphs from an HTML file. The empty paragraphs are those <p></p> elements without textContent.

HTML File with Empty Paragraphs:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!--
This page is used with remove_empty_paragraphs.php script.
This page contains empty paragraphs. The script removes the empty paragraphs and
writes a new HTML file.
-->
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <p>This is a paragraph.</p>
        <!-- Below is an empty paragraph. -->
        <p><span></span></p>
        <p>This is another paragraph.</p>
        <!-- Below is another empty paragraph. -->
        <p class=MsoNormal><b></b></p>
        <p style=''></p>
        <p>
            <span lang=EN-US style='font-size:5.0pt;color:navy;mso-ansi-language:EN-`US'></span>
        </p>
    </body>
</html>

First Attempt:

$html = new DOMDocument("1.0", "UTF-8");
$html->loadHTMLFile("HTML File with Empty Paragraphs.html");
$pars = $html->getElementsByTagName("p");

/* removeChild foreach-loop */
foreach ($pars as $par) {
    if ($par->textContent == "") {
        $par->parentNode->removeChild($par);
    }
}

$html->saveHTMLFile("HTML File WithOut Empty Paragraphs.html");

This succeeds to:

  • remove empty paragraphs without the style-attribute,

but fails to:

  • remove empty paragraphs with the
    style-attribute.

So I insert the removeStyleAttribute foreach-loop before the removeChild foreach-loop. (I do not mind removing the style-attributes of nonempty paragraphs.)

Second Attempt:

$html = new DOMDocument("1.0", "UTF-8");
$html->loadHTMLFile("HTML File with Empty Paragraphs.html");
$pars = $html->getElementsByTagName("p");    

/* removeStyleAttribute foreach-loop */
foreach ($pars as $par) {
    if ($par->hasAttribute("style")) {
        $par->removeAttribute("style");
    }
}

/* removeChild foreach-loop */
foreach ($pars as $par) {
    if ($par->textContent == "") {
            $par->parentNode->removeChild($par);
    }
}

$html->saveHTMLFile("HTML File WithOut Empty Paragraphs.html");

This succeeds in:

  • removing the style-attributes from empty paragraphs which have the style attribute.
  • removing empty paragraphs that do not have the style-attributes.

But fails! to:

  • remove those empty paragraphs from which the style-attributes were
    removed.

So I have to have two removeChild foreach-loops, one after the other.

Third Attempt:

$html = new DOMDocument("1.0", "UTF-8");
$html->loadHTMLFile("HTML File with Empty Paragraphs.html");
$pars = $html->getElementsByTagName("p");

/* removeStyleAttribute foreach-loop */
foreach ($pars as $par) {
    if ($par->hasAttribute("style")) {
        $par->removeAttribute("style");
    }
}

/* First removeChild foreach-loop */
foreach ($pars as $par) {
    if ($par->textContent == "") {
        $par->parentNode->removeChild($par);
    }
}

/* Second removeChild foreach-loop, identical to the first removeChild foreach-loop */
foreach ($pars as $par) {
    if ($par->textContent == "") {
        $par->parentNode->removeChild($par);
    }
}

$html->saveHTMLFile("HTML File WithOut Empty Paragraphs.html");

This works perfectly!, but it is weird to have two identical loops, one right after the other.

I also tried to use only one loop for everything.

Fourth Attempt:

$html = new DOMDocument("1.0", "UTF-8");
$html->loadHTMLFile("HTML File with Empty Paragraphs.html");
$pars = $html->getElementsByTagName("p");  

foreach ($pars as $par) {
    if ($par->textContent == "") {
        if ($par->hasAttribute("style")){
            $par->removeAttribute("style");
        }
        $par->parentNode->removeChild($par);
    }
}

$html->saveHTMLFile("HTML File WithOut Empty Paragraphs.html");

This succeeds to:

  • remove empty paragraphs without the
    style-attribute,

but fails to:

  • remove the style-attribute from empty paragraphs that have it.
  • remove empty paragraphs with the style attribute.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:42:15+00:00Added an answer on May 16, 2026 at 6:42 pm

    The list returned by getElementsByTagName is dynamic: removing nodes from the document also removes them from the list. And since foreach doesn’t know the list changed, it’ll happily move to the next item – which is actually two items down because the DOMNodeList was rearranged. Some of the <p> tags were just plain skipped.

    Solution: use a for loop (with $pars->item(X) and $pars->length) instead of a foreach, but only increment if a node was not deleted. (Or always increment and backtrack if one was deleted.)

    Separately: the last <p> (with the large <span>) wasn’t deleted because of the whitespace around the <span>. Use trim() to get rid of it.

    See also my reply in http://forums.devnetwork.net/viewtopic.php?f=1&t=121114&p=623974.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php script (generator.php) which generates certain HTML/XML/SVG depending on various conditions.
I have this PHP script which i'm grabbing images from a directory and displaying
Here I have a simple php script which displays some values from a database
In a php-script i have a form, method is post, action-attribute is empty, which
I have a php script $filelist = scandir('myfolder/') which list outs files from my
On my site, I have a PHP script which takes multiple source files (HTML,CSS,
I have a PHP script (news-generator.php) which, when I include it, grabs a bunch
I have a php script which saves the original image, then resizes it -
I have a PHP script which creates and listens on a port for connections
I have a PHP script which is generating buttons to each content schedule. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.