I am able to parse another page using DOM. I am able to retrieve hrefs, imgs and so
on. How should i b able to parse this?
<span class="abc up" id="price">+9395</span>
I am able to parse another page using DOM. I am able to retrieve hrefs, imgs and so
on. How should i b able to parse this?
<span class="abc up" id="price">+9395</span>
Assuming you have the DOMElement, you get the value by accessing the nodeValue property... Example below:
<?php
$doc = new DOMDocument();
$doc->loadHTML('<span class="abc up" id="price">+9395</span> ');
$elements = $doc->getElementsByTagName('span');
echo $elements->item(0)->nodeValue;
I assumed you had found the node already... As Alistair says you could use XPath.
http://de.php.net/manual/en/domxpath.query.php
$xpath = new DOMXPath($doc);
$spans = $xpath->query('//span[@id="price"]');
echo $spans->item(0)->nodeValue;
To determine the Xpath you can use various modern browsers and look for a unique path to the desired element.
strip_tags
will work on a string. You don't have a string, you have a document.
For nontrivial parsing of HTML (or XML), you'll need something that can intelligently traverse the DOM, like DOMDocument or QueryPath, or XPath, etc. However, for very trivial cases—and this appears to be one—you can simply use strip_tags
:
echo strip_tags('<span class="abc up" id="price">+9395</span>');
Produces +9395
.
strip_tags
must contain nothing but the value you want for this to work. If, for example, you pass it <span>+9395</span><p>some other data</p>
it'll return +9395some other data
, which you don't want.
Or you could do it using XPath too:
<?php
$html = '<span class="abc up" id="price">+9395</span>';
$document = new DOMDocument();
$document->loadHTML($html);
$xpath = new DOMXPath($document);
$results = $xpath->query('//span');
foreach($results as $result) {
echo $result->nodeValue . PHP_EOL;
}
That will show all values for the span elements. If you wanted to search by id you'd use //span[@id="price"] and by class //span[@class="abc up"]
use simple html dom
//turn the html into a dom object:
$html = str_get_html('<span class="abc up" id="price">+9395</span>');
//find the first element with id "price":
$node->find('#price', 0);
//grab its inner text:
echo $node->innertext;
it uses css3 style selectors, which is nice