0

I am trying to get a data field using PHP Simple HTML DOM Parser. I can pull the links, images etc but cannot get a certain data attribute.

Example HTML -

<div id="used">
    <div id="srpVehicle-1C3CCCEG2FN601809" class="vehicle" data-vin="1C3CCCEG2FN601809">
    <div id="srpVehicle-1C3CCCEG2FN601810" class="vehicle" data-vin="1f2CfCEG2FN266778">
</div>

I would like to get all the "data-vin" fields on a site.

Here is my go at it -

$html = file_get_html($url);

foreach($html->find("div[data-vin]", 0) as $vin){
  echo $vin."<br>";
}

But it returns the whole page when I echo $vin. How can I access that data-vin field?

3
  • Looking at the documentation I think you want to use: $html->find("div[data-vin]", 0). Commented May 2, 2019 at 19:16
  • I would use domdocument and an xpath. Commented May 2, 2019 at 19:19
  • Thank you, you are correct. It returns the whole page though when I echo $vin Commented May 2, 2019 at 19:20

1 Answer 1

1
$html->find("data-vin", 0)

is looking for tags named data-vin, when you really want tags with the attribute data-vin.

foreach($html->find("[data-vin]") as $tag){
    echo $tag->getAttribute('data-vin')."<br>";
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you but it gives me the error: Uncaught Error: Call to a member function getAttribute() on integer
@RyanD I left in something incorrect from the original code. Please see edit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.