views:

331

answers:

3

I have a URL as a string. How do I match the numbers after the VideoID. Also VideoID may occur at different points in the URL. But I will worry about that afterwards, as I can't even do this.

$string = 'http://example.com/index.php?action=vids.individual&VideoID=60085484';

preg_match('/(?<VideoID>)=/', $string, $matches);

print_r($matches);

...Spare some change for a noob. :)

+1  A: 
/(?:\?|&)VideoID=([0-9]+)/   # get just the ID, stored in \\1
/(?:\?|&)(VideoID=[0-9]+)/   # get VideoId=ID, stored in \\1

Under the assumption that your URL is properly formed, it will always be preceded by either ? or &, and with your example the URL is strictly numerical, so it will match a valid ID up to the next segment of the URL.

Ian Elliott
`\d == [0-9]`. Just my OCD tendencies lol.
tj111
Alan Moore
A: 
$string = 'http://example.com/index.php?action=vids.individual&amp;VideoID=60085484&amp;somethingelse';
$s = explode("VideoID=",$string);
print preg_replace("/[^0-9].*/","",$s[1]);
ghostdog74
Will fail if VideoID is not at the end of the URL
Ian Elliott
+3  A: 

Just use the built-in parse_url/parse_str

$string = 'http://example.com/index.php?action=vids.individual&amp;VideoID=60085484';
$URL = parse_url($string);
parse_str($URL['query'],$Q);
print_r($Q);

Array (
[action] => vids.individual
[VideoID] => 60085484 )

Eddy