1

If I have the following html:

<div id="thisID">100</div>

I can get the value, 100, like this:

$dom = new DOMDocument();
$dom->loadHTMLFile($url);
$data = $dom->getElementById("thisID");
$result = $data->nodeValue;`

But what about this html?

<span class="foo" id="bar" itemprop="price">100</span>

Is there a way I can get an element content by a tag variable and value, in this case itemprop="price"?

2

1 Answer 1

0

a) Use DOMXPath:

<?php

$doc = new DOMDocument();
$doc->loadHTML('<span class="foo" id="bar" itemprop="price">100</span>');

$xpath = new DOMXPath($doc);
$result = $xpath->evaluate('number(//*[@itemprop="price"])');

b) Use a real microdata parser.

Sign up to request clarification or add additional context in comments.

Comments

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.