I use the following php code to get a random amount of URL’s form an xpath query:
@$dom = new DOMDocument();
@$dom->loadHTML( $rawPage );
@$xpath = new DOMXPath( $dom );
@$itemCells = $xpath->query( "//td[@width=120]/a" );
I need to pick a url randomly from that pool so I can visit it via cURL.
What I would like to do is get the count of how many URL’s are found so I can use that as the max in a rand( 0 , $itemCells->length )
However it tells me that $itemCell Cannot use object of type DOMNodeList as array and that my rand() rand() expects parameter 2 to be long, object given
Perhaps there is a better way to go about this.
I suspect that since
$lengthis areadonlyproperty, you’d run into trouble passing it to a function. So, the solution is to save the$lengthvalue from aDOMNodeListto a local variable first, then callrand(), like so:Then you should be able to do this to grab a random node from the list:
And from there, to grab the URL, you would do: