Skip to content

Instantly share code, notes, and snippets.

@toomanyredirects
Forked from olets/domain-from-url.twig
Last active September 9, 2019 14:59
Show Gist options
  • Save toomanyredirects/51948e6266507980dafc72af511367db to your computer and use it in GitHub Desktop.
Save toomanyredirects/51948e6266507980dafc72af511367db to your computer and use it in GitHub Desktop.
get the domain from a url
{# Twig URL parser: #}
{% set url= 'http://subdomain.domain.tld/some/dir/file.pdf' %}
{% set noprotocol = url|split('//')[1] ?: url %}
{% set domains = noprotocol|split('/')[0] ?: noprotocol %}
{% if domains|split('.')[2] is defined %}
{% set subdomain = domains|split('.')[0] %}
{% set domain = domains|split('.')[1] %}
{% set tld = domains|split('.')[2] %}
{% else %}
{% set subdomain = '' %}
{% set domain = domains|split('.')[0] %}
{% set tld = domains|split('.')[1] %}
{% endif %}
{% set path = noprotocol|replace({(subdomain ? subdomain ~ '.' : '') ~ (domain) ~ '.' ~ (tld) :''}) %}
{{ domains }}
{{ domain }}
{{ subdomain }}
{{ tld }}
{{ path }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment