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? 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? 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.. Commented Feb 16, 2011 at 5:43
  • phpQuery ftw. code.google.com/p/phpquery 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.

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

10 Comments

@Jacob: What if there are many spans ...??? Is it possible to do a combination of, say: -- getElementsBy(TagName or Class) and getElementById ???
@Jacob: Ori's seems to be a one-liner... simple
I thought you were looking within a larger document for a specific span and extracting the value.
strip_tags will work on a string. You don't have a string, you have a document.
but what if i open the file and assign it to a handle and then use it in --strip_tags-- ...???
|
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 Comments

that was gr8... simple and perfect
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.
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 Comment

What if there are many spans ...??? Is it possible to do a combination of, say: -- getElementsBy(TagName or Class) and getElementById ??
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

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.