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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:56:56+00:00 2026-06-13T05:56:56+00:00

I am trying to put into a string the HTML after a h1 tag

  • 0

I am trying to put into a string the HTML after a h1 tag up until the next h1 tag, and then continue.

For example, here is the HTML:

<h1>Heading</h1>
<p>Paragraph</p>
<ul>
  <li>List item</li>
  <li>List item</li>
</ul>
<p>Paragraph</p>
<h1>Heading 2</h1>
<ul>
  <li>List item</li>
  <li>List item</li>
</ul>
<p>Paragraph<img /></p>

And from this I am trying to create this array:

array(
  0 => '<p>Paragraph</p><ul><li>List item</li><li>List item</li></ul><p>Paragraph</p>',
  1 => '<ul><li>List item</li><li>List item</li></ul><p>Paragraph<img /></p>'
)

What would be the XPath query to select all content after the h1 tag up until the next, and so on?

Any help or advice is appreciated.

UPDATE:

What I am ultimately trying to achieve is, using PHP, create this format of an array:

array(
  'headings' => array(
      1 => '<h1>Heading</h1>',
      2 => '<h1>Heading 2</h1>'
  ),
  'content'  => array(
      1 => '<p>Paragraph</p><ul><li>List item</li><li>List item</li></ul><p>Paragraph</p>',
      2 => '<ul><li>List item</li><li>List item</li></ul><p>Paragraph<img /></p>'
  )
)
  • 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-06-13T05:56:57+00:00Added an answer on June 13, 2026 at 5:56 am

    I did it like this 🙂

    $html = '<h1>Heading</h1><p>Paragraph</p><ul><li>List item</li><li>List item</li></ul><p>Paragraph</p><h1>Heading 2</h1><ul><li>List item</li><li>List item</li></ul><p>Paragraph<img /></p>';
    
    $dom_document = new DOMDocument();
    
    $dom_document->loadHTML($html);
    $dom_document->preserveWhiteSpace = false;
    
    //use DOMXpath to navigate the html with the DOM
    $dom_xpath = new DOMXpath($dom_document);
    
    $elements = $dom_xpath->query("/html/body/*");
    
    if (!is_null($elements)) {
        $i = 0;
        foreach ($elements as $element) {
            if ($element->nodeName == 'h1') {
                $i++;
                $array['headings'][$i] = $dom_document->saveHtml($element);
                continue;
            } else {
                $array['content'][$i] .= $dom_document->saveHtml($element);
            }
        }
    }
    
    var_dump($array);
    

    NOTE: should you be using PHP 5.2 then replace:

    $array['headings'][$i] = $dom_document->saveHtml($element);
    

    and

    $array['content'][$i] .= $dom_document->saveHtml($element);
    

    with:

    $array['headings'][$i] = $dom_document->saveXml($element);
    $array['content'][$i] .= $dom_document->saveXml($element);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to put a html table into the body of an email.
I've got a Roo generated webapp that I'm trying to put JQuery into (I
I am trying to put 2 columns into a website, I've got that to
I'm trying put an if statement directly into a select field in rails, with
I'm trying to put some custom list adapters into their own classes to make
I'm trying to put a 3D model into a PDF file. When I load
I'm trying to put some sites i crawled into a shelve, but the shelve
I am trying to put the following code into VBA. What I ideally want
I'm trying to put together an image uploader. I want to upload images into
I'm trying to take the output of arc4sin and put it into a label.

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.