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

Ask A Question

Stats

  • Questions 533k
  • Answers 533k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer RewriteCond %{REQUEST_URI} !bounce/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule… May 17, 2026 at 12:32 am
  • Editorial Team
    Editorial Team added an answer I would check what you get as 'position' and compare… May 17, 2026 at 12:32 am
  • Editorial Team
    Editorial Team added an answer A fulltext index does not help to speed up queries… May 17, 2026 at 12:32 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a php script which saves the original image, then resizes it -
I have a (PHP) script which runs on a new row in MySQL. However,
I have a script that generates a csv file using the following code: header('Content-type:
I have the following build.xml file setup in phpUnderControl. <target name=phpunit> <exec executable=phpunit dir=${basedir}/httpdocs
Say I have two drop downs which are populated when the my jsp loads
I'm trying to edit a php contact form script, but I can't find where
I coded a php page that displays information from a mysql database neatly into
I have a form containing some dynamic elements. These are basically text input boxes
I have a bunch of HTML that is generated by a daemon using C,
Hi there I have this code NSLog(@%@,URLRequestQueryString); NSString *sendToServerString = [NSString stringWithFormat:@http://mydomain.co.uk/req.php%@,URLRequestQueryString]; NSURL *sendToServer

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.