Search code examples
phphtmlxpathhtml-content-extraction

get the URL to the last page of a webpage


$doc = new DOMDocument();
libxml_use_internal_errors(true);
@$doc->loadHTMLFile('http://www.mudah.my/Malaysia/Electronics-3000/directd-for-sale?o=1&q=directd&th=1'); 

$xpath = new DOMXPath($doc);

$nlist = $xpath->query('//*[@id="list_ads_container"]/div[1]/div[3]/span[1]/a/div');

echo $nlist->item(0)->textContent;

I'm trying to get the URL of the last page of the webpage, but at last what I get is a word "Last" and not the URL.


Solution

  • URL is set in href attribute of <a>. So you don't need to select the last <div> and get the attribute content like

    $nlist = $xpath->query('//*[@id="list_ads_container"]/div[1]/div[3]/span[1]/a');
    $href  = $nlist->item(0)->getAttribute('href');