i want to extract id
from URL Like URL is https://www.themoviedb.org/movie/49730-red-riding-hood?language=en-US
it should be like $id = 49730
Asked
Active
Viewed 75 times
0

Ram Sharma
- 60
- 1
- 8
-
2Possible duplicate of [Laravel 5 getting ID from URL](https://stackoverflow.com/questions/33266316/laravel-5-getting-id-from-url) – Jeto May 25 '19 at 08:06
-
Can you post some code around what you have tried till now? – Muthukrishnan May 25 '19 at 11:38
-
Problem is solved you can check the answer again. – Ram Sharma May 26 '19 at 11:37
1 Answers
1
Are you looking for this?
$url = 'https://www.themoviedb.org/movie/49730-red-riding-hood?language=en-US';
$urlFromBrowser = $_SERVER['HTTP_HOST']; // or you can get the url from the browser
$id = preg_split('#([0-9]+)#', $url, null, PREG_SPLIT_DELIM_CAPTURE)[1];
echo $id;
// ALTERNATIVE REGEX
$id2 = str_replace('-', '', preg_split('#([0-9]+[-]{1})#', $url, null, PREG_SPLIT_DELIM_CAPTURE)[1]); // this REGEX takes in case the '-' after the number so it can be better to check with this one if the URL might contain multiple numbers at different positions
echo $id2;

tcj
- 1,645
- 4
- 13
- 21