Skip to content

Instantly share code, notes, and snippets.

@mark05e
Last active January 7, 2025 16:54
Show Gist options
  • Save mark05e/55d1f1572ea87070723b3c47c34c5602 to your computer and use it in GitHub Desktop.
Save mark05e/55d1f1572ea87070723b3c47c34c5602 to your computer and use it in GitHub Desktop.

Extract Base Url

FUNCTION extract_base_url(input_url)
{
  // Split the input URL by protocol ("://")
  url_components = input_url.split("://")

  // Check if the split resulted in at least two parts (protocol and domain)
  IF (url_components.count() >= 2) 
  {
    // Extract the protocol and hostname
    protocol = url_components[1] 
    hostname_and_path = url_components[2]
    hostname_and_path_array = hostname_and_path.split("/")
    hostname = hostname_and_path_array[1]

    // Construct the base URL
    base_url = "{protocol}://{hostname}"

    // Return the extracted base URL
    RETURN base_url 
  } ELSE 
  {
    // Handle cases where the URL is invalid or does not contain a protocol
    RETURN "" 
  }
}

Extract Path

FUNCTION extract_path(input_url)
{
  // Split the input URL by protocol ("://")
  url_components = input_url.split("://")

  // Check if the split resulted in at least two parts (protocol and domain)
  IF (url_components.count() >= 2) 
  {
    // Extract the remaining URL part (everything after the protocol)
    remaining_url = url_components[2] 

    // Split the remaining URL part by "/"
    url_parts = remaining_url.split("/")

    // Initialize an empty string to store the path
    path = ""

    // Loop through the URL parts (starting from the second element)
    FOR i = 2 TO url_parts.count() 
    {
      // Append a "/" to the path
      path.append("/") 

      // Append the current URL part to the path
      path.append(url_parts[i]) 
    }

    // Return the constructed path
    RETURN path 
  } ELSE 
  {
    // Handle cases where the URL is invalid or does not contain a protocol
    RETURN "" 
  }
}

Test Functions

FUNCTION run_extract_tests()
{
	input_url = "https://elementor.com/blog/website-url/"
	result1 = extract_base_url(input_url)
	result2 = extract_path(input_url)
	TRACE "extract_base_url: {result1}"
	TRACE "extract_path: {result2}"
}
run_extract_tests()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment