hamza hamza - 4 months ago 9
PHP Question

How to extracting a sub <a> tags and prints them out

I trying to extract links from a sub

<a>
tag that is inside a
<div>
element. I have used PHP's DOM to parse HTML explained at this site: [http://htmlparsing.com/php.html][1]. I also modified the code using a related answer from [Using PHP DOM document, to select HTML element by its class and get its text to select elements using class name. Following are HTML structure and PHP code.However,the PHP code does not seems to be working well as it stops printing the links once it reach the 11th elements.

HTML STRUCTURE:

<div class="avtar-abt">
<h3 class="mb6"><a href="testingwebsite.com"></i></a></h3>
</div>


PHP CODE:

# Create a DOM parser object
$dom = new DOMDocument();

# Parse the HTML from Google.
# The @ before the method call suppresses any warnings that
# loadHTML might throw because of invalid HTML in the page.
@$dom->loadHTML($html);
$xpath = new DOMXPath ($dom);

$classname = 'mb6';

foreach ($xpath->query("//*[@class='$classname']/a") as $link) {
echo $link->getAttribute('href');
echo "<br />";

}

Answer

You should not use two loops (the first one has a wrong syntax BTW). You can use XPath to get right to the link nodes, by adding /a to the search path:

foreach ($xpath->query("//*[@class='$classname']/a") as $link) {
    echo $link->getAttribute('href');
    echo "<br />";
}   
Comments