简体   繁体   中英

Get pagination's last page value with php file_get_contents

I am looking a way to the find the last value of pagination of an external website. With using get_file_contents I am getting the following result but how can I get the the value "9" just before the Next

DOMElement Object ( [tagName] => ul 
[schemaTypeInfo] => [nodeName] => ul 
[nodeValue] => Prev 1 2 3 4 .. 9 Next 
[nodeType] => 1 [parentNode] => (object value omitted) 
[childNodes] => (object value omitted) 
[firstChild] => (object value omitted) [lastChild] => (object value omitted) 
[previousSibling] => [attributes] => (object value omitted) 
[ownerDocument] => (object value omitted) 
[namespaceURI] => [prefix] => [localName] => ul [baseURI] => 
[textContent] => Prev 1 2 3 4 .. 9 Next )

If I were you, I would first split the textContent with spaces. Then the answer will be [Next Key - 1].

$textContent = " Prev 1 2 3 4 .. 9 10 Next ";
$arr = explode(' ', $textContent);

if ($key = array_search('Next', $arr)) {
echo $arr[$key - 1];  
}
// output : 10

If there is always a Next at the end of the string, you might want to preg_match() with the following RegExp : ([0-9]+) Next on nodeValue key like this :

$found = preg_match("#([0-9]+) Next#", $domElement['nodeValue'], $matches)

Then if ($found) {} check what's in $matches[1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM