Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the ID from exploding url

Tags:

php

Currently I have the following code.

$file_id = 'https://skyvault.co/show/file?filename=6N2viQpwLKBIA6';
$parts = parse_url($file_id);
$path_parts = explode('/', $parts[path]);
$secret = $path_parts[3];
print $secret;

Above you can see that I am trying to explode by / and it's not returning the output I am looking for it's just returning file and I need it to return 6N2viQpwLKBIA6 so how could I get that ID?

like image 394
Ritzy Avatar asked Feb 01 '16 14:02

Ritzy


Video Answer


2 Answers

parse_url works but you need to specify the query index. Redo it like this:

$file_id = 'https://skyvault.co/show/file?filename=6N2viQpwLKBIA6';
$parts = parse_url($file_id);
$path_parts = explode('=', $parts['query']);
$secret = $path_parts[1];
print $secret;
like image 135
CodeGodie Avatar answered Sep 29 '22 08:09

CodeGodie


Is it possible for you to do it quickly this way, if the URL is always same for all the items?

$URLComp = explode("show/file?filename=", $file_id);
$secret = $URLComp[1];
print $secret;

The main reason is, there could be cases of with or without www, then with or without https.

like image 25
Praveen Kumar Purushothaman Avatar answered Sep 29 '22 09:09

Praveen Kumar Purushothaman