0

Possible Duplicate:
Grabbing the href attribute of an A element

I'm currently trying to parse a webpage for a specific element which will be in this format:

<div id="main-id">
    <div id="sub-id-1" onclick="some onclick"> 
    <span class="big-class" style="some style">
    </span>
    <div id="sub-id-2">&#160;</div>
</div>

The main part I'm trying to pull from this is the entire <span class="big-class" style="some style"> tag, as I need to pull the style from the element and store it to a string. To do this, I tried using the following code:

$dom = new DOMDocument();
$dom->validateOnParse = true;
$dom->loadHTML($html);

$belement = $dom->getElementById("main-id");
echo $belement->nodeValue;

However, this is only returning the character Â, which is what the character code &#160 is for.

I'm not really sure what to search for in order to accomplish this, and I'm not even sure if pull entire HTML lines(?) with DOM. Is there any way I could use DOM to return this span element?

Community
  • 1
  • 1
John
  • 293
  • 2
  • 6
  • 18
  • In other words, use this XPath query: `//span[@class="big-class"]` – Gordon Oct 07 '12 at 17:24
  • Also see http://schlitt.info/opensource/blog/0704_xpath.html – Gordon Oct 07 '12 at 17:24
  • And to get the entire node serialized, e.g. it's outerHTML, see http://stackoverflow.com/questions/5404941/php-domdocument-outerhtml-for-element/5404962#5404962 – Gordon Oct 07 '12 at 17:29
  • Using the code above, you should get the fragment you're after, no? Try viewing source to see if the HTML bits are being printed in the src of the page. – krg Oct 07 '12 at 17:46

1 Answers1

1

nodeValue will return the innerHTML for your tag. So, in this case it rightly returns Â. You can check PHP Xpath : get all href values that contain needle for a similar discussion. You can do this:

$html = <<< HTML
<div id="main-id">
    <div id="sub-id-1" onclick="some onclick"> 
    <span class="big-class" style="some style">
    </span>
    <div id="sub-id-2">&#160;</div>
</div>
HTML;

$xml  = simplexml_load_string($html);
$span_elem = $xml->xpath('//span[@class="big-class"]');
Community
  • 1
  • 1
automaticAllDramatic
  • 2,025
  • 1
  • 21
  • 25
  • I'm not sure how to incorporate that since I already have `$dom->loadHTML($html)`, could you show me an example? – John Oct 07 '12 at 17:10
  • While you correctly explained the cause, the solution is wrong. – Gordon Oct 07 '12 at 17:20
  • @Gordon Yes, the solution is incorrect. I am updating my answer... :D – automaticAllDramatic Oct 07 '12 at 17:34
  • Thank you rizwaniqbal, however, I'm still having some issues getting this to work. When I try to output the string that I'm grabbing with `$xml->xpath`, I just get text that says `Array`, and using `print_r($span_elem);`, gives me `Array ( )`. Am I missing something obvious about storing this into a string? Thank you. – John Oct 07 '12 at 18:34