Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get only id from url

Tags:

url

php

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

like image 895
Sufyan Avatar asked Jan 08 '23 23:01

Sufyan


2 Answers

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

like image 57
Sougata Bose Avatar answered Jan 15 '23 21:01

Sougata Bose


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];
like image 29
OptimusCrime Avatar answered Jan 15 '23 21:01

OptimusCrime