简体   繁体   English

从链接数组中过滤出最高整数

[英]Filtering out highest integer from an array of links

But of a confusing title so let me explain. 但是标题令人困惑,所以让我解释一下。 I have an array of links like this: 我有这样的链接数组:

http://somesite.com/videoplayback?ip=81.0.0.0&sparams=id,expire,ip,ipbits,itag,algorithm,burst,factor&fexp=905602&algorithm=throttle-factor&itag=34&ipbits=8&burst=40&sver=3&expire=1285056000&key=yt1&signature=690F9475D5288F3129F84364427B2B490B6ACE59.45C8F83DEE3DD361855B12AE538EA6349FF8EF9B&factor=1.25&id=d50e6528eb51ad54,18

http://somesite.com/videoplayback?ip=81.0.0.0&sparams=id,expire,ip,ipbits,itag,algorithm,burst,factor&fexp=905602&algorithm=throttle-factor&itag=18&ipbits=8&burst=40&sver=3&expire=1285056000&key=yt1&signature=A68EAA3F7A2ECA2BB2BD6C35BF443C03E4BB1172.AD2FF9FDAF046B23F789FE1A7F7882DF9A355DE4&factor=1.25&id=d50e6528eb51ad54,5

http://somesite.com/videoplayback?ip=81.0.0.0&sparams=id,expire,ip,ipbits,itag,algorithm,burst,factor&fexp=905602&algorithm=throttle-factor&itag=5&ipbits=8&burst=40&sver=3&expire=1285056000&key=yt1&signature=ABC8ACF6899C46CC992ECB5F6A6FD7E66383EA3D.0C8B707083203DC1153FB26586A94BFAC64D176B&factor=1.25&id=d50e6528eb51ad54

If you look at the very end of those URL's, they have extensions like ,18 , ,5 and there is one last link with no extension like this at all. 如果您查看这些URL的末尾,则它们的扩展名为,18,5并且最后一个链接根本没有这样的扩展名。

Now, I need to use the link that has the highest number on the end as possible later in my code. 现在,我需要在代码的后面尽可能使用结尾处具有最高编号的链接。 In this example I need to filter out the very first link, because it has the highest integer on the end ( 18 ). 在此示例中,我需要过滤掉第一个链接,因为它的末尾( 18 )具有最高的整数。

I would use a series of if() blocks, but in this case the integers on the end can change so that's not a good solution. 我会使用一系列的if()块,但是在这种情况下,最后的整数可能会更改,所以这不是一个好的解决方案。

So I basically need to go through my array, check which link has the highest integer at the end (note that it is only 2 digits in length) and then store it in another variable. 因此,我基本上需要遍历数组,检查哪个链接的末尾具有最高的整数(请注意,它的长度只有2位数字),然后将其存储在另一个变量中。

Can anyone provide some sample/psudo code on how to effectively do this? 谁能提供一些样本/伪代码来说明如何有效地做到这一点?

Cheers. 干杯。

This will work even if there are commas in other places in the URL: 即使URL的其他位置有逗号,这也将起作用:

$links = array("http://somesite.com/videoplayback?ip=81.0.0.0&sparams=id,expire,ip,ipbits,itag,algorithm,burst,factor&fexp=905602&algorithm=throttle-factor&itag=34&ipbits=8&burst=40&sver=3&expire=1285056000&key=yt1&signature=690F9475D5288F3129F84364427B2B490B6ACE59.45C8F83DEE3DD361855B12AE538EA6349FF8EF9B&factor=1.25&id=d50e6528eb51ad54,18", "http://somesite.com/videoplayback?ip=81.0.0.0&sparams=id,expire,ip,ipbits,itag,algorithm,burst,factor&fexp=905602&algorithm=throttle-factor&itag=18&ipbits=8&burst=40&sver=3&expire=1285056000&key=yt1&signature=A68EAA3F7A2ECA2BB2BD6C35BF443C03E4BB1172.AD2FF9FDAF046B23F789FE1A7F7882DF9A355DE4&factor=1.25&id=d50e6528eb51ad54,5", "http://somesite.com/videoplayback?ip=81.0.0.0&sparams=id,expire,ip,ipbits,itag,algorithm,burst,factor&fexp=905602&algorithm=throttle-factor&itag=5&ipbits=8&burst=40&sver=3&expire=1285056000&key=yt1&signature=ABC8ACF6899C46CC992ECB5F6A6FD7E66383EA3D.0C8B707083203DC1153FB26586A94BFAC64D176B&factor=1.25&id=d50e6528eb51ad54");

$max = 0;
$highestLink = "";
foreach ($links as $link) {
    $data = explode(",", strrev($link));
    $val = strrev($data[0]);
    if (is_numeric($val)) {
        $val = (int) $val;
        if ($val > $max) {
            $max = $val;
            $highestLink = $link;
        }
    }
}

echo $max;

For the obfuscation award: 对于混淆奖:

array_multisort(
    array_map('intval',preg_replace('/^.*?(,([0-9]+))?$/','$2',$array)),
    $array);
echo end($array);

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

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