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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:39:50+00:00 2026-06-16T15:39:50+00:00

i am trying to catch all the images on a page using Xpath and

  • 0

i am trying to catch all the images on a page using Xpath and then iterating through the node list checking if the image has attribute if it does i iterate through the attributes till i get to src now my problem is when i get relative paths like /us/english/images/12/something.jpeg or something like that.. my question is: is there a way go get the full path ?

I thought of regex the returned src and look for host if host isn’t there use the site’s url but that can be hard to check for..

i also thought maybe i should parse url and check for [‘host’] part if the host part has “.”dot meaning there is host and i shouldn’t add it ?

Here is what i have so far:

$image_list = $xpath->query('//img');
    foreach($image_list as $element){
        if($element->hasAttributes()){
            foreach($element->attributes as $attribute){
                if(strtolower($attribute->nodeName) == 'src'){
                    echo $attribute->nodeName. ' = ' .$attribute->nodeValue.'<br>';
                }

            }
        }
    }

would appreciate any help.

  • 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-16T15:39:52+00:00Added an answer on June 16, 2026 at 3:39 pm
    1. Change your xpath query to //img[src]. This will return all the img elements that has src attribute. Use getAttribute method.your code will be shorter and efficient.

      $image_list = $xpath->query("//img[@src]");
      for($i=0;$i<$image_list->length; $i++){
          echo "src = ". $image_list->item($i)->getAttribute("src"). "\n";
      }
      
    2. About the relative paths problem, you should find the base elements href attribute. If its found use it as base URI for relative urls. If its not found try to find the URL of this document. That’ll be the base URI.

    Update

    As you want to read the image file path in the complex url like

    //lp.hm.com/hmprod?set=key[source],value[/environment/2012/P01_2972_044R_0.‌​‌​jpg]&amp;set=key[rotate],value[0.65]&amp;set=key[width],value[2921]&amp;set=key[‌​h‌​eight],value[3415]&amp;set=key[x],value[1508]&amp;set=key[y],value[495]&amp;se‌​t=k‌​ey[type],value[FASHION_FRONT]&amp;call=url[file:/product/large]
    

    you better use a custom parser like this,

    $url = $image_list->item($i)->getAttribute("src");
    $q = strpos($url, "?");
    $query = substr($url, $q+1);
    $params = explode("&", html_entity_decode($query));
    $data = array();
    foreach($params as $e){
        if(preg_match("/key\[([^\]]+)\],value\[([^\]]+)\]/", $e, $m))
            $data[$m[1]]=$m[2];
        elseif(preg_match("/call=([^\[]+)\[([^\]]+)\]/", $e, $m))
            $data[$m[1]]=$m[2];
    }
    
    print_r($data);
    

    CodePad

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

Sidebar

Related Questions

I am trying to extract all the images from a pdf using itextsharp but
Im working on a response filter. Im trying to catch all expresion: $sometext.sometext$ with
What I am trying is to catch all the form submit events, get the
I'm trying to write some code to catch all Errors and ErrorEvents however i
I am trying to make a catch-all proxy that takes any selector with any
I'm trying to catch all unhandled exceptions in my app in order to conditionally
I am trying to build a robust, simple, safe, catch-all way to pass dynamic
I'm trying to add base, small, and thumbnail images to all of my products
I have an Activity with three images. When one image is clicked, all the
I am trying to convert multiple images to pdf using pdfsharp library . I

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.