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

  • Home
  • SEARCH
  • 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 7623939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:49:57+00:00 2026-05-31T04:49:57+00:00

Hoping this is possible with Simple Html Dom, I’m scraping a page that looks

  • 0

Hoping this is possible with Simple Html Dom, I’m scraping a page that looks like this:

<h5>this is title 1</h5>
<img>
<img>
<img>

<h5>this is title 2</h5>
<img>
<img>

<h5>this is title 3</h5>
<img>
<img>
<img>
<img>

etc…

I’m trying to get it to look something like:

<h5>this is title 1</h5>
<img>
<h5>this is title 1</h5>
<img>
<h5>this is title 1</h5>
<img>


<h5>this is title 2</h5>
<img>
<h5>this is title 2</h5>
<img>

Which means for each IMG I need to find and grab the first previous H5, I think. There’s no parent divs or any structure to make it any easier, it’s pretty much how I described it.

The code I’m using looks something like this (simplified):

foreach($html->find('img') as $image){

//do stuff to the img

$title = $html->find('h5')->prev_sibling();


echo $title; echo $image;}

Everything I’ve tried with prev_sibling gets me a “Fatal error: Call to a member function prev_sibling() on a non-object” and I’m wondering if what I’m trying to do is even possible with PHP Simple HTML Dom. I hope so, all the other scrapers I’ve tried were making me pull my hair out.

  • 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-31T04:49:59+00:00Added an answer on May 31, 2026 at 4:49 am

    Essentially, you want to select all h5 elements, as well as all the img elements. Then, you loop through them, and check their type. If it’s an h5 element, you update your $title variable but don’t echo anything. If it’s an img, you simply echo the $title before the image. No need to go hunting for the h5 now since you’ve already cached it.

    Here’s an example:

    foreach ( $html->find('h5, img') as $el )
    {
        if ( $el->tag == 'h5' )
        {
            $title = $el->plaintext;
            continue;
        }
    
        echo "<h5>$title</h5>";
        echo $el->outertext;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hoping that using something like this demo it is possible to drag items within
I've been looking around the internet hoping that this is possible, I basically need
I'm hoping someone can help me. I'm using PHP Simple HTML DOM Parser (http://simplehtmldom.sourceforge.net/manual.htm)
It's possible that this question either has a simple answer that's a standard practice,
Not sure if this is possible in one Makefile alone, but I was hoping
I know this was not possible in C++03, but I'm hoping there is some
This may not be possible, but I'm hoping someone will have an idea how
I am hoping this is a simple fix, or maybe my logic is just
I'm hoping this problem is a fairly simple one as I am relatively new
I'm hoping this question has a very simple answer. I can think of ways

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.