开发者

regex part of url

So I'm trying to get the id from a url fo开发者_C百科r youtube..

here is the url

http://gdata.youtube.com/feeds/api/videos/kffacxfA7G4/related?v=2

then there's also - in the url too.

it wouldn't let me post another url but it's the same as above but with the id ucvkO0x-mL4

how can I grab between videos/ and /related (the id) with regex?

I tried to use txt2re.com which is what I always use, but it's not working for this case..

thanks!


No need for even regex, just a simple strpos and substr will do it. Or just use explode like this:

<?php

$url = 'http://gdata.youtube.com/feeds/api/videos/kffacxfA7G4/related?v=2';

//BY STRPOS/SUBSTR
echo substr($url, 42, strpos($url, '/related', 42) - 42);

//BY EXPLODE
$parts = explode('/', $url);
echo $parts[6];
?>


(?:.*)videos/(.*?)/related\?v=2


This is how I would do it

$url = 'http://gdata.youtube.com/feeds/api/videos/kffacxfA7G4/related?v=2';
preg_match('#.*videos/([0-9a-zA-Z_\-]{11})/related.*#', $url, $matches);
print_r($matches);

But, @shamittomar is right about strpos and substr


using regex you can do it by

$url = "http://gdata.youtube.com/feeds/api/videos/kffacxfA7G4/related?v=2";

preg_match('/videos\/(.+)\/related/',$url,$match);

$id = $match[1];

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜