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);