简体   繁体   中英

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.

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:

$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 .

Note that this assumes your HTML entities are properly escaped, otherwise DOMDocument will issue a warning.

One optimization you can do is instead of keeping an array of end values, just compare the max value seen with the current value. However this will only be useful if the number of <a> tags grows larger.

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 .

$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 )

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:

$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);
}

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