简体   繁体   English

PHP正则表达式-找到最高价值

[英]PHP regex - Find the highest value

I need find the highest number on a string like this: 我需要在这样的字符串上找到最高的数字:

Example

<div id='pages'>
 <a href='pages.php?start=0&end=20'>Page 1</a>
 <a href='pages.php?start=20&end=40'>Page 2</a>
 <a href='pages.php?start=40&end=60'>Page 3</a>
 <a href='pages.php?start=60&end=80'>Page 4</a>
 <a href='pages.php?start=80&end=89'>Page 5</a>
</div>

In this example, I should get 89, because it's the highest number on "end" value. 在此示例中,我应该得到89,因为它是“结束”值上的最高数字。

I think I should use regex, but I don't know how :( 我想我应该使用正则表达式,但是我不知道如何:(

Any help would be very appreciated! 任何帮助将不胜感激!

You shouldn't be doing this with a regex. 您不应该使用正则表达式来执行此操作。 In fact, I don't even know how you would. 实际上,我什至不知道你会怎么做。 You should be using an HTML parser, parsing out the end parameter from each <a> tag's href attribute with parse_str() , and then finding the max() of them, like this: 应该使用HTML解析器,使用parse_str()从每个<a>标记的href属性中解析出end参数,然后找到它们的max() ,如下所示:

$doc = new DOMDocument;
$doc->loadHTML( $str); // All & should be encoded as &amp; 
$xpath = new DOMXPath( $doc);
$end_vals = array();
foreach( $xpath->query( '//div[@id="pages"]/a') as $a) {
    parse_str( $a->getAttribute( 'href'), $params);
    $end_vals[] = $params['end'];
}
echo max( $end_vals);

The above will print 89 , as seen in this demo . 本演示所示,以上将打印89

Note that this assumes your HTML entities are properly escaped, otherwise DOMDocument will issue a warning. 请注意,这假设您的HTML实体已正确转义,否则DOMDocument将发出警告。

One optimization you can do is instead of keeping an array of end values, just compare the max value seen with the current value. 您可以做的一种优化方法是,不保留end值的数组,而只是将看到的最大值与当前值进行比较。 However this will only be useful if the number of <a> tags grows larger. 但是,这仅在<a>标签数量增加时有用。

Edit: As DaveRandom points out, if we can make the assumption that the <a> tag that holds the highest end value is the last <a> tag in this list, simply due to how paginated links are presented, then we don't need to iterate or keep a list of other end values, as shown in the following example . 编辑:作为DaveRandom指出,如果我们可以假设<a>持有最高标签end值是最后<a>在此列表标签,简单地归结于如何分页链接呈现,那么我们不需要迭代或保留其他end值的列表,如以下示例所示。

$doc = new DOMDocument;
$doc->loadHTML( $str); 
$xpath = new DOMXPath( $doc);
parse_str( $xpath->evaluate( 'string(//div[@id="pages"]/a[last()]/@href)'), $params);
echo $params['end'];

To find the highest number in the entire string , regardless of position, you can use 查找整个字符串中的最高数字 ,无论位置如何,都可以使用

Example ( demo ) 范例( demo

echo max(preg_split('/\D+/', $html, -1, PREG_SPLIT_NO_EMPTY)); // prints 89

This works by splitting the string by anything that is not a number , leaving you with an array containing all the numbers in the string and then fetching the highest number from that array. 这是通过将字符串除以不是数字的任何东西来工作的,从而为您提供一个包含字符串所有数字的数组,然后从该数组中获取最大的数字。

first extract all the numbers from the links then apply max function: 首先从链接中提取所有数字,然后应用max函数:

$str = "<div id='pages'>
 <a href='pages.php?start=0&end=20'>Page 1</a>
 <a href='pages.php?start=20&end=40'>Page 2</a>
 <a href='pages.php?start=40&end=60'>Page 3</a>
 <a href='pages.php?start=60&end=80'>Page 4</a>
 <a href='pages.php?start=80&end=89'>Page 5</a>
</div>";

if(preg_match_all("/href=['][^']+end=([0-9]+)[']/i", $str, $matches))
{
    $maxVal = max($matches[1]);
    echo $maxVal;
}
function getHighest($html) {
    $my_document = new DOMDocument();
    $my_document->loadHTML($html);
    $nodes = $my_document->getElementsByTagName('a');
    $numbers = array();

    foreach ($nodes as $node) {
        if (preg_match('\d+$', $node->getAttribute('href'), $match) == 1) {
            $numbers[]= intval($match[0])
        }
    }

    return max($numbers);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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