prabh
prabh

Reputation: 45

Accessing a class object in PHP

Does anyone has idea how to access HREF from the below object:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [title] => Preview
            [rel] => enclosure
            [type] => image/jpeg
            [href] => http://a1.phobos.apple.com/us/r1000/008/Purple/94/ee/38/mzl.fupornmt.320x480-75.jpg
        )

)

Upvotes: 0

Views: 119

Answers (4)

Alex Jasmin
Alex Jasmin

Reputation: 39506

SimpleXML objects representing XML elements support array like access to the element attributes

(string)$simpleXMLElement['href']

The (string) cast is needed if you wish to compare the attribute with a string or pass it into a function that requires a string. Otherwise, PHP treats the attribute as an object. (That applies if you use the attributes() method as well)

Upvotes: 3

ssice
ssice

Reputation: 3683

I can't say "Vote Up", but as Anpher says, you just need to access the attribute:

$attrs = $obj->attribues(); // Gets you the "[@attributes]" array (which is a copy of the internal private property "attributes")

do_things_with($attrs['href']); // Accesses the element you want.

Upvotes: 0

Marcis
Marcis

Reputation: 4617

Use SimpleXMLElement::attributes method:

$attrs = $obj->attributes();
echo $attrs['href'];

Upvotes: 2

hummingBird
hummingBird

Reputation: 2555

Use simpleXML http://php.net/manual/en/book.simplexml.php

Upvotes: 0

Related Questions