4

The problem is retrieving and displaying the div contents. Catch is, it's with a multiclass:

<li class="activity user-generated" id="activity:153411947-kIq0nOFam0hE68sL6P298_DIo4s-1386284074745">

I probably could've worked with it easier if the ID doesn't automatically generate for each new status, in my case.

Also, on this topic, is it possible to get each tag and list them in order?

Thanks for your time.

$classname = "activity";
$domdocument = new DOMDocument();
$domdocument->loadHTML($doc);
$a = new DOMXPath($domdocument);
$spans = $a->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]");

with $doc being the page I'm loading

Alright, let me restart this, since the fact this isn't working is bugging me greatly.

What I'm trying to do is read from the element I've posted, but I need to call it from a URL, as reading just the element itself as HTML is fine. From a URL, I get a bunch of unnecessary errors, basically repeating the same thing.

Function obtainUserStatus($Username){
    $DOM = new DOMDocument();
    $Class = "activity";
    $DOM->loadHTMLFile('http://lifestream.aol.com/stream/' . $Username);
    $X = new DOMXPath($DOM);
    $ID = $X->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $Class ')]")->item(0)->getAttribute('id');
    Echo $ID;
}

Which just returns the errors.

I need to return the div's content from the class (or the ID, which I'm doing from there -- no big deal.)

user2943677
  • 131
  • 9
  • Have you tried anything? If so, show what you've tried. If not, try something before asking a question. – Paul Dessert Dec 06 '13 at 00:36
  • 1
    Yes, I've tried literally everything you could probably think of, but let me edit it quick. – user2943677 Dec 06 '13 at 00:37
  • 1
    Take a look here, it might help... http://stackoverflow.com/questions/5662404/how-can-i-select-an-element-with-multiple-classes-with-xpath – Paul Dessert Dec 06 '13 at 00:45
  • 1
    and +1 for editing your question and adding what you've tried. 99.9% of people don't do that. – Paul Dessert Dec 06 '13 at 00:45
  • 1
    Thanks, but I did look there. That didn't work for me either :| It doesn't return anything. – user2943677 Dec 06 '13 at 00:47
  • Your code works for me. What version of PHP are you using? – Phil Dec 06 '13 at 01:47
  • 1
    The latest version -- did you try it with the top element? Also, I'm requesting it from a URL with file_get_contents, could that be a problem? – user2943677 Dec 06 '13 at 01:49
  • *"The latest version"* - that's cute. **What version of PHP are you using?** – Phil Dec 06 '13 at 01:50
  • 1
    5.5, DOM is supported. – user2943677 Dec 06 '13 at 01:50
  • Working demo here using 5.5 - http://codepad.viper-7.com/OhMRJn – Phil Dec 06 '13 at 01:52
  • Also, you don't need to use `file_get_contents()`. You can load a remote HTML file using [`DOMDocument::loadHTMLFile()`](http://php.net/manual/domdocument.loadhtmlfile.php) (not that it makes much difference) – Phil Dec 06 '13 at 02:00
  • 1
    Hmm, interesting. What about this though?; Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: ID ls-headerSearch already defined in Entity I'm trying to get it through file_get_contents, and when I try it with just plain HTML, it's fine. – user2943677 Dec 06 '13 at 02:00
  • @user2943677 Not sure what you mean by *plain HTML*. Check out these questions regarding that warning - http://stackoverflow.com/questions/415927/domdocument-ignore-duplicate-element-ids and http://stackoverflow.com/questions/3614586/dom-error-id-someanchor-already-defined-in-entity-line – Phil Dec 06 '13 at 02:03
  • 1
    By plain HTML, I mean just the element itself, not by trying to load it through the URL (since that's the only way I'll be able to effectively use it.) – user2943677 Dec 06 '13 at 02:06
  • Did you read the two linked posts I added above? They have solutions for dealing with HTML errors that appear as DOMDocument warnings – Phil Dec 06 '13 at 03:34
  • Those entity related things and so many other errors I faced recently when I tried DOMDoc. I switched to regex and it was a breeze. But, it may be your requirements! – Satya Prakash Dec 06 '13 at 06:54
  • Yeah, I was trying RegEx, just the bad thing is retrieving the multiple classes -- I'd also like to keep my code neat but it's definitely my backup. @Phil Yeah I did, but they weren't exactly of help. Thanks anyway, though. – user2943677 Dec 06 '13 at 16:58

1 Answers1

0

Okay, I've finally resolved this.

For anyone who needs the solution (4 likes so I'd guess a few people do), now of course our situations are different but this should definitely help you:

Function getDivContents($URL, $ID){
    $DOM = new DOMDocument();
    $DOM->validateOnParse = True;
    @$DOM->loadHtml(File_Get_Contents($URL));
    $xPath = new DOMXPath($DOM);
    $Main = $DOM->getElementById($ID)->nodeValue;

    Return $Main;
}

And just call it with Echo getDivContents('url', 'id');

[edited to fit minimal needs, if any errors come up just comment]

user2943677
  • 131
  • 9