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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:21:19+00:00 2026-06-04T04:21:19+00:00

What I’m looking at doing is essentially the same thing a Tweet button or

  • 0

What I’m looking at doing is essentially the same thing a Tweet button or Facebook Share / Like button does, and that is to scrape a page and the most relevant title for a piece of data. The best example I can think of is when you’re on the front page of a website with many articles and you click a Facebook Like Button. It will then get the proper information for the post relative to (nearest) the Like button. Some sites have Open Graph tags, but some do not and it still works.

Since this is done remotely, I only have control of the data that I want to target. In this case the data are images. Rather than retrieving just the <title> of the page, I am looking to somehow traverse the dom in reverse from the starting point of each image, and find the nearest “title”. The problem is that not all titles occur before an image. However, the chance of the image occurring after the title in this case seems fairly high. With that said, it is my hope to make it work well for nearly any site.

Thoughts:

  • Find the “container” of the image and then use the first block of text.
  • Find the blocks of text in elements that contain certain classes (“description”, “title”) or elements (h1,h2,h3,h4).

Title backups:

  • Using Open Graph Tags
  • Using just the <title>
  • Using ALT tags only
  • Using META Tags

Summary: Extracting the images isn’t the problem, it’s how to get relevant titles for them.

Question: How would you go about getting relevant titles for each of the images? Perhaps using DomDocument or XPath?

  • 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-04T04:21:20+00:00Added an answer on June 4, 2026 at 4:21 am

    Your approach seems good enough, I would just give certain tags / attributes a weight and loop through them with XPath queries until I find something that exits and it’s not void. Something like:

    i = 0
    
    while (//img[i][@src])
      if (//img[i][@alt])
        return alt
      else if (//img[i][@description])
        return description
      else if (//img[i]/../p[0])
        return p
      else
        return (//title)
    
      i++
    

    A simple XPath example (function ported from my framework):

    function ph_DOM($html, $xpath = null)
    {
        if (is_object($html) === true)
        {
            if (isset($xpath) === true)
            {
                $html = $html->xpath($xpath);
            }
    
            return $html;
        }
    
        else if (is_string($html) === true)
        {
            $dom = new DOMDocument();
    
            if (libxml_use_internal_errors(true) === true)
            {
                libxml_clear_errors();
            }
    
            if ($dom->loadHTML(ph()->Text->Unicode->mb_html_entities($html)) === true)
            {
                return ph_DOM(simplexml_import_dom($dom), $xpath);
            }
        }
    
        return false;
    }
    

    And the actual usage:

    $html = file_get_contents('http://en.wikipedia.org/wiki/Photography');
    
    print_r(ph_DOM($html, '//img')); // gets all images
    print_r(ph_DOM($html, '//img[@src]')); // gets all images that have a src
    print_r(ph_DOM($html, '//img[@src]/..')); // gets all images that have a src and their parent element
    print_r(ph_DOM($html, '//img[@src]/../..')); // and so on...
    print_r(ph_DOM($html, '//title')); // get the title of the page
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has

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.