PHP DOM
Results 1 to 7 of 7

Thread: PHP DOM

  1. #1
    Junior Member
    Join Date
    Mar 2009
    Posts
    28

    PHP DOM

    How to get the value of "title" using php DOM Document?


    PHP Code:
    <div class='lovers_list'><a href="/laetizia.coronet" class="avatar_tiny tooltip s " title="laetizia.coronet"><img alt="laetizia.coronet" class="avatar" height="24px" src="https://my-secondlife.s3.amazonaws.com/users/laetizia.coronet/tiny_sl_image.png?5d0d6c32c74d8bbe6a606279ef95dee5" width="24px" /></a
    so far ive got..


    PHP Code:
    $dom = new DOMDocument;
    @
    $dom->loadHTMLFile('file.html');
    $divs=$dom->getElementsByTagName('div');
    foreach(
    $divs as $div){  
     if (
    $div->getAttribute('class') == "lovers_list")   {   


    thank you

  2. #2
    Senior Member
    Join Date
    Aug 2008
    Location
    London, UK
    Posts
    753
    Does the $div variable contain a reference to the correct div in your if statement? I often find it helpful to use var_dump() on objects which are coming from things like this, as it lets you know what child objects, etc, it contains, and gives you and idea of how you can reference them. I've not used DOMDocument myself, but I would imagine that you could get the link object with:
    PHP Code:
    $a $div->getElementsByTagName('a'); 
    but I'm sure there's a more efficient and better way of doing this.
    Ashley Sheridan
    www.ashleysheridan.co.uk

  3. #3
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,883
    Quote Originally Posted by Ashley Sheridan
    but I'm sure there's a more efficient and better way of doing this.
    You could for example use the language designed for DOM navigation.

    PHP Code:
    $xpath = new DOMXPath($dom);
    $titles $xpath->evaluate("//div[@class='lovers_list']/a/@title"$dom);
    foreach(
    $titles as $title)
    {
        
    $title_value $title->textContent;
        
    var_dump($title_value);

    It could be simpler if there is exactly one "title" (as suggested by the wording of the original post).
    PHP Code:
    $title $xpath->evaluate("string(//div[@class='lovers_list']/a/@title)"$dom);
    var_dump($title); 
    Last edited by Weedpacket; 10-25-2012 at 09:17 AM.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  4. #4
    Junior Member
    Join Date
    Mar 2009
    Posts
    28
    thank you both for your replies..

    removed reply as WeedPacket solved the issue in his Edit while i was replying..

    Thank you, have a great day
    Last edited by BeanieMan; 10-25-2012 at 09:30 AM.

  5. #5
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,714
    And if every anchor tag has a title, it'd be something like:

    PHP Code:
    $dom = new DOMDocument
    @
    $dom->loadHTMLFile('file.html'); 
    $anchors=$dom->getElementsByTagName('a'); 
    foreach(
    $anchors as $a){   
     echo 
    $a->getAttribute('title');    

    Got a RESOLVED tag for this yet?
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  6. #6
    Junior Member
    Join Date
    Mar 2009
    Posts
    28
    I realise that there is more than 1 instance of "lovers_list" div on the page.. i only wish to evaluate the 1st instance of "lovers_list".
    Please can anyone modify WeedPackets excelent Xpath evalution to only return the 1st "lovers_list" ?

    PHP Code:
    $xpath = new DOMXPath($dom); 
    $titles $xpath->evaluate("//div[@class='lovers_list']/a/@title"$dom); 
    foreach(
    $titles as $title

        
    $title_value $title->textContent
        
    var_dump($title_value); 

    Thank you

  7. #7
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,883
    Code:
    //div[@class='lovers_list']/a/@title[1]
    and see also the shorter code in my first reply.

    Alternatively, note that $titles is a DOMNodeList, the interface of which, though small, has everything necessary for the task.
    Last edited by Weedpacket; 10-25-2012 at 07:19 PM.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •