Created
November 14, 2021 18:11
-
-
Save BenRogersWPG/3ce8ba97652234eb2d106158b3e73796 to your computer and use it in GitHub Desktop.
Find URL Slug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def slugFinder(urlString): | |
slashSplit = urlString.split('/') | |
if len(slashSplit[-1]) < 1: | |
# If the trailing slash exists, just return the item before that, as the trailing slash contains 0 characters after it | |
if len(slashSplit[3]) == 0: | |
return '/' | |
else: | |
return slashSplit[-2] | |
else: | |
# if no trailing slash, determine if it is the root, by comparing the last item (presumed slug) vs the TLD value. If they are the same, then we are at the root | |
if slashSplit[-1] == slashSplit[2]: | |
return '/' | |
else: | |
return slashSplit[-1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment