2

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>  
4
  • 1
    what do you want to get out of it?
    – Jacob
    Commented Feb 16, 2011 at 5:35
  • Is it in an HTML document, or by itself? We need to see a sample case. Do you identify it by it's class or id?
    – Jonah
    Commented Feb 16, 2011 at 5:38
  • it is a separate html doc... which i return to my page..(I do an ajax post from my page).. identifying in either ways would be fine..
    – ineedhelp
    Commented Feb 16, 2011 at 5:43
  • phpQuery ftw. code.google.com/p/phpquery
    – Jason
    Commented Feb 16, 2011 at 5:53

4 Answers 4

3

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.

10
  • @Jacob: What if there are many spans ...??? Is it possible to do a combination of, say: -- getElementsBy(TagName or Class) and getElementById ???
    – ineedhelp
    Commented Feb 16, 2011 at 5:47
  • @Jacob: Ori's seems to be a one-liner... simple
    – ineedhelp
    Commented Feb 16, 2011 at 5:55
  • I thought you were looking within a larger document for a specific span and extracting the value.
    – Jacob
    Commented Feb 16, 2011 at 5:58
  • strip_tags will work on a string. You don't have a string, you have a document.
    – Jacob
    Commented Feb 16, 2011 at 6:10
  • but what if i open the file and assign it to a handle and then use it in --strip_tags-- ...???
    – ineedhelp
    Commented Feb 16, 2011 at 6:17
1

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.

2
  • that was gr8... simple and perfect
    – ineedhelp
    Commented Feb 16, 2011 at 5:54
  • Trivial cases because the strings you are passing to 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.
    – Ori
    Commented Feb 16, 2011 at 6:10
0

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"]

1
  • What if there are many spans ...??? Is it possible to do a combination of, say: -- getElementsBy(TagName or Class) and getElementById ??
    – ineedhelp
    Commented Feb 16, 2011 at 5:48
0

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.