views:

25

answers:

2

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.

A: 

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;
SimpleCoder
That doesn't put anything in `$highestLink`, I'll look through it in a sec :) Thanks. PS: What does the `!!` in the `if()` statement mean? If it's `NOT NOT` isn't it redundant? Thanks.
Will
It does through these lines: `if ($val > $max) { ... $highestLink = $link;` Although I changed my code (see the edits) I was using `!!` incorrectly. I meant to say `strstr(",", $line) === false` instead. You can't just use `==` because if `,` is found at position 0, `0 == false` so the condition would be triggered incorrectly. `===` ensures the value is `false` and not `0`.
SimpleCoder
A: 

For the obfuscation award:

array_multisort(
    array_map('intval',preg_replace('/^.*?(,([0-9]+))?$/','$2',$array)),
    $array);
echo end($array);
Wrikken
sort + end = max ;)
stereofrog
Yes, but multisort is by reference, so `max(boolean true)` => `true` would fail. And the OP doesn't want `max`, he wants _'the url with specific condition max'_
Wrikken