开发者

Regex to parse megavideo URL

I'm trying to write a regex to parse a this url for a php script:

http://www.megavideo.com/v/B4PZHP0Nb2e8a877f8378e778446318596415780

to get this: B4PZHP0N

Can someone help? Th开发者_运维知识库anks in advance.


Since you're in PHP, just use parse_url and substr:

$mega = 'http://www.megavideo.com/v/B4PZHP0Nb2e8a877f8378e778446318596415780';
$want = substr(parse_url($mega, PHP_URL_PATH), 3, 8);

Demo: http://ideone.com/f3viH


Try this regex:

/^http:\/\/www\.megavideo\.com\/v\/(.{8}).*$/

(The error has been corrected)

Also see my ideone or my jsfiddle.


/([^:.\/]+)[a-f0-9]{32}/

So if it matches, B4PZHP0N is in capture buffer 1, ie: $1


I have done something similar but a bit more generic. so the id can come either after /v/, ?v= or &v=

$url = 'http://www.megavideo.com/v/B4PZHP0Nb2e8a877f8378e778446318596415780';
foreach (array('/v/', '?v=', '&v=') as $k)
{
    $pos = strpos($url, $k);
    if ($pos>0)
    {
        $pos += strlen($k);
        break;
    }   
}   
if (!$pos)
    die("not found");
$id = substr($url, $pos, 8);
die($id);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜