HTML中具有最高值的抓取参数 [英] Grab parameter in html with highest value
本文介绍了HTML中具有最高值的抓取参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 $ html
:
$html = '<div>
<a href="./?pg=99"></a>
<a href="./?pg=32"></a>
<a href="./?pg=95"></a>
<a href="./?pg=1"></a>
//etc
';
我如何浏览它并获取 pg的最高价值
?因此,在上述情况下,我们得到:
How do I skim through it and grab the highest value of pg
? So in above case, we get:
$result = 99;
可能的方法是通过 preg_match_all()
和正则表达式查找 /?pg =
案例,对它们进行排序并获取最高的数值。
Potential approach is to via preg_match_all()
and regex locate /?pg=
cases, order them and grab the highest numeric value.
推荐答案
您需要转义 ./?
(导致 \.\ / \?
)字符以正确获取数字,对连续链接使用 U
标志,然后使用 max()
:
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
无需订购数字。
这篇关于HTML中具有最高值的抓取参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文