dtziv24262 2010-09-21 01:17
浏览 33
已采纳

过滤掉链接数组中的最高整数

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.

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

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.

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.

Can anyone provide some sample/psudo code on how to effectively do this?

Cheers.

  • 写回答

2条回答 默认 最新

  • drza10046 2010-09-21 01:23
    关注

    This will work even if there are commas in other places in the 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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办