4

I have a URL:

$url = 'https://docs.google.com/spreadsheets/d/1ljJpZDiayzMLhIJ-JDSIJjdjdY_xg3RrUDljFVRB0Qk/edit?usp=sharing#helloworld';

I want to get the ID out of this URL. The ID is always the longest part of the URL 1ljJpZDiayzMLhIJ-JDSIJjdjdY_xg3RrUDljFVRB0Qk, so my approach is to target the longest part.

How can I break this URL into parts and grab the longest part? I want to ignore the query variable part ?usp=sharing#helloworld when breaking it into parts.

What I've tried so far

I tried a preg_match_all() approach with a regex that doesn't seem to break the URL properly:

$regex = '/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/';
$url = 'https://docs.google.com/spreadsheets/d/1ljJpZDiayzMLhIJ-JDSIJjdjdY_xg3RrUDljFVRB0Qk/edit?usp=sharing#helloworld';
$result = preg_match_all($regex, $url, $matches);
print($matches);
  • remove `https://docs.google.com/` using `str_replace` and explode the string by "/" and find the index where your id is stored. – hungrykoala Jun 09 '17 at 12:12
  • 2
    `parse_url()` and / or `explode()`. – jeroen Jun 09 '17 at 12:12
  • Avoid regex as much as possible. Split URL with `/` and find longest string in array -> https://stackoverflow.com/q/3713517 – hjpotter92 Jun 09 '17 at 12:13
  • @hjpotter92 Sounds viable, but I want to ignore the `?usp=sharing#helloworld` part because occasionally, it is longer than the ID. Please feel free to post an answer. – Goodbye World Jun 09 '17 at 12:14
  • @hungrykoala The URL doesn't always start with `https://docs.google.com/` -- it was only an example. – Goodbye World Jun 09 '17 at 12:15
  • @jeroen Please feel free to post an answer. Note that `https://docs.google.com/` may be different sometimes and query variable `?usp=sharing#helloworld` needs to be removed when checking the longest part. – Goodbye World Jun 09 '17 at 12:17

5 Answers5

8

You can use the function explode to split a string in a array.

And the function parse_url()to get the path of your URL.

$path = parse_url($url, PHP_URL_PATH);
$array = explode("/", $path);

edit

If you want to include query-variables you can add this three lines.

parse_str($query,$queries);
$query = parse_url($url, PHP_URL_QUERY);
$array = array_merge($array, $queries);

Now you can look wich part is the longest.

$id = "";
foreach($array as $part){
    if(strlen($id) < strlen($part)) {
        $id = $part;
    }
}
Y4roc
  • 1,066
  • 8
  • 12
1
$url = 'https://docs.google.com/spreadsheets/d/1ljJpZDiayzMLhIJ-JDSIJjdjdY_xg3RrUDljFVRB0Qk/edit?usp=sharing#helloworld';
$partURL=explode('/', $url);
$lengths = array_map('strlen', $partURL);
$maxLength = max($lengths);
$index = array_search($maxLength, $lengths);
echo $partURL[$index];

Returns: 1ljJpZDiayzMLhIJ-JDSIJjdjdY_xg3RrUDljFVRB0Qk

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Charly
  • 101
  • 1
  • 8
0

You can use this regex: ^.*\/d\/(.*)\/.*$ For example:

$regex = '/^.*\/d\/(.*)\/.*$/';
$url = 'https://docs.google.com/spreadsheets/d/1ljJpZDiayzMLhIJ-JDSIJjdjdY_xg3RrUDljFVRB0Qk/edit?usp=sharing#helloworld';
$result = preg_match_all($regex, $url, $matches);
print_r($matches);

in result you'll have:

Array
(
    [0] => Array
        (
            [0] => https://docs.google.com/spreadsheets/d/1ljJpZDiayzMLhIJ-JDSIJjdjdY_xg3RrUDljFVRB0Qk/edit?usp=sharing#helloworld
        )

    [1] => Array
        (
            [0] => 1ljJpZDiayzMLhIJ-JDSIJjdjdY_xg3RrUDljFVRB0Qk
        )

)
kRicha
  • 797
  • 9
  • 27
  • This doesn't work with all URLs. For example `https://drive.google.com/open?id=1ljJpZDiayzMLhTZcywUGyVkC2Y_xg3RrUDljFVRB0Qkss2` Therefore, I need to break the URL in parts, no matter its setup first, and then grab the longest part (ignoring the query variable). – Goodbye World Jun 09 '17 at 12:20
  • @GoodbyeWorld you can't ignore query part in second example, because id is a part of query. You need to specify types of links and use different parsers for different url types. – kRicha Jun 09 '17 at 12:26
0

you can use substr(string,start,length)

Hinal
  • 35
  • 7
0

You can filter parse_url result with PHP_URL_QUERY, for example

$query = parse_url(<url string>, PHP_URL_QUERY);
parse_str($parts, $queryArray);
$queryArray[<KEY>]
atrichkov
  • 450
  • 3
  • 6