Search code examples
domxpathdomparserxpathquery

DOMDocument xpath query cut off withing a specific timeframe ?


is there a way to cut of the query and return back the results.. preset time to take to get all images from the given url .. i.e. query half of the webpage ? or job time not greater than 5 seconds, so therefore, it will get everything it can in 5 seconds.....

$xpath = new DOMXPath( $htmlget);
       $nodelist = $xpath->query( "//img/@src" );

Solution

  • You can evaluate separately any of the following XPath expressions one by one, and stop this process whenever a timer expires or other criterion is satisfied:

    (//img/@src)[1]
    (//img/@src)[2]
    (//img/@src)[3]
    ...............
    (//img/@src)[$N]
    

    This can probably be speeded-up by chunking:

    (//img/@src)[position() < 100]
    (//img/@src)[position() >= 100 and position() < 200]
    ...............
    (//img/@src)[position() >= 100*$N and position() < 200*$N]