开发者

Parse html using php

I am able to parse another page using DOM. I am able to retrieve h开发者_如何学编程refs, 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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜