I have $html
:
$html = '<div>
<a href="./?pg=99"></a>
<a href="./?pg=32"></a>
<a href="./?pg=95"></a>
<a href="./?pg=1"></a>
//etc
';
How do I skim through it and grab the highest value of pg
? So in above case, we get:
$result = 99;
Potential approach is to via preg_match_all()
and regex locate /?pg=
cases, order them and grab the highest numeric value.
You will need to escape ./?
(resulting in \.\/\?
) characters to get the numbers correctly, use the U
flag for consecutive links, and then use max()
:
// number --v U --v
preg_match_all('/<a.*href="\.\/\?pg=(\d+)".*>(?:.*)<\/a>/U', $html, $matches);
// numbers are our capture group 1 --> $matches[1]
$result = max($matches[1]);
var_dump($result);
// 99
No need to order the numbers.