Sufyan
Sufyan

Reputation: 516

How to get only id from url

I have thousands of urls which have ids i want to get only ids from url for example This is my array

Array
(
    [0] => http://www.videoweed.es/file/f62f2bc536bad
    [1] => http://www.movshare.net/video/5966fcb2605b9
    [2] => http://www.nowvideo.sx/video/524aaacbd6614
    [3] => http://vodlocker.com/pbz4sr6elxmo
)

I want ids from above links

f62f2bc536bad

5966fcb2605b9

524aaacbd6614

pbz4sr6elxmo

I have use parse_url function but its return me path which include all things after slash(/) like /file/pbz4sr6elxmo

<?php 
foreach($alllinks as $url){
  $parse = parse_url($url);
  echo $parse['path'];
}
?>

Output

/pbz4sr6elxmo

/video/5966fcb2605b9

/file/f62f2bc536bad

/video/524aaacbd6614

Upvotes: 4

Views: 360

Answers (3)

Vijay Porwal
Vijay Porwal

Reputation: 152

Try this:

$alllinks = array(
    'http://www.videoweed.es/file/f62f2bc536bad',
    'http://www.movshare.net/video/5966fcb2605b9',
    'http://www.nowvideo.sx/video/524aaacbd6614',
    'http://vodlocker.com/pbz4sr6elxmo'
);

foreach($alllinks as $url){
  $parts = explode('/', $url);
  echo end($parts).'<br/>';
}

Upvotes: 0

Sougata Bose
Sougata Bose

Reputation: 31739

You can try with explode -

$alllinks = array
(
    'http://www.videoweed.es/file/f62f2bc536bad',
    'http://www.movshare.net/video/5966fcb2605b9',
    'http://www.nowvideo.sx/video/524aaacbd6614',
    'http://vodlocker.com/pbz4sr6elxmo'
);

foreach($alllinks as $url){
  $temp = explode('/', $url);
  echo $temp[count($temp) - 1].'<br/>';
}

Output

f62f2bc536bad
5966fcb2605b9
524aaacbd6614
pbz4sr6elxmo

This will only help if the the url structure is same, i.e. the last part is the id

Upvotes: 6

OptimusCrime
OptimusCrime

Reputation: 14863

If the URLs always ends with the id you can simply do

$url = 'http://www.videoweed.es/file/f62f2bc536bad';
$url_split = explode('/', $url);
$code = $url_split[count($url_split) - 1];

Upvotes: 3

Related Questions