XPATH/PHP – Smarter way to acommplish this?

I have the following:

$html = "<a href="/path/to/page.html" title="Page name"><img src="path/to/image.jpg" alt="Alt name"  />Page name</a>" 

I need to extract href and src attribute and anchor text

My solution:

$dom = new DOMDocument;
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('a') as $node) { 
    $href = $node->getAttribute('href');
    $title = $node->nodeValue;
}
foreach ($dom->getElementsByTagName('img') as $node) { 
    $img = $node->getAttribute('src');
}

What would be the smarter way?

Leave a Reply

*

Hire Me
Follow Me!
Search
Most Popular Articles & Pages
Because your vote is Important
Sorry, there are no polls available at the moment.
Categories