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 4042618
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:01:42+00:00 2026-05-20T13:01:42+00:00

I am trying to get HTML in this pattern… <p>a</p> <p>b</p> <p>c</p> … <h3>title</h3>

  • 0

I am trying to get HTML in this pattern…

<p>a</p>
<p>b</p>
<p>c</p>
...
<h3>title</h3>

<p>e</p>
<p>e</p>
<p>f</p>
...
<h3>title2</h3>
...

…and turn it into…

<ul>
  <li>
     <blockquote>
        <p>a</p>
        <p>b</p>
        <p>c</p>
        <cite>title</cite>
     </blockquote>
  </li>
  <li>
     <blockquote>
        <p>d</p>
        <p>e</p>
        <p>f</p>
        <cite>title2</cite>
     </blockquote>
  </li>
</ul>

The PHP code I have is…

$dom = new DOMDocument('1.0', 'utf-8');

$dom->preserveWhiteSpace = FALSE;

$dom->loadHTML($content);

$ul = $dom->createElement('ul');

$body = $dom->getElementsByTagName('body')->item(0);

$blockquote = FALSE;

foreach($body->childNodes as $element) {

    if ($element->nodeType != XML_ELEMENT_NODE) {
        continue;
    }

    if ( ! $blockquote) {
        $blockquote = $dom->createElement('blockquote');
        $li = $dom->createElement('li');
    }

    switch ($element->nodeName) {

        case 'p':
            $blockquote->appendChild($element);

            break;
        case 'h3':
            $li->appendChild($blockquote);

            $ul->appendChild($li);

            $blockquote = $li = FALSE;
            break;

    }
}

$body->appendChild($ul); 
echo $dom->saveHTML();

Whilst the functionality was not finished, I noticed that the loop stopped when I added $blockquote->appendChild($element).

If I remove all the appendChild stuff, the loop works fine.

My guess is that by moving the current element in the iteration, it breaks the loop.

How would I get this to work?

  • 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-20T13:01:43+00:00Added an answer on May 20, 2026 at 1:01 pm

    If it’s choking when inserting into the current document, have you considered creating a new document instead? You can use the importNode method on DOMDocument to copy just the correct bits across from the old document into the right structure you’ll create in the new document.

    You might be able to use a similar trick using a document fragment, only using serialized XML instead of working with node objects. This could work for the paragraph tags, at least.

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

Sidebar

Related Questions

I am trying to do is to loop this HTML and get a nested
I'm trying to get the height of a html window's content. This is the
I'm trying to get a HTML source of a website through C# code. When
I am trying to get an HTML-based recursive directory listing based on code here:
I'm trying to get some HTML to work with my python code. I've got
I have this xpath pattern: tags = doc.xpath('/html/body//a[text() = ' + name.encode('utf8') + ']/@href'
I am trying to get the src from this jquery html(). So using jquery
I'm trying to parse strings that represent source code, something like this: [code lang=html]
I am trying get all html links within a string and replace them using
I am trying get all html links within a string and replace them using

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.