Guide
URL Parsing
Wordsmith provides several helpful methods for working with URLs.
The getUrlParts()
function parses a string as a URL and attempts to return an array of component parts — e.g. protocol, hostname, path, etc.
(Several shortcut filters are also available if you only need to extract one specific part.)
You can also parse the video ID out of YouTube URLs. (Vimeo URL parsing coming soon.)
Wordsmith’s name parsing methods include:
URL parsing
parseUrl
Parses a string as a URL and returns a hash of its components:
{% parseUrl('https://michael@guidesr.us:42/the-answer/?foo=bar#anchor') %} |
This function takes after PHP’s parse_url()
method, except that it always includes all of the possible components, even if they’re empty. Furthermore, the individual components returned by parseUrl
are string objects, to preserve chainability.
urlFragment
Returns the fragment component of a URL. (i.e. the part after a hashmark ‘#’, which usually represents an anchor on the target page.)
{{ 'https://michael@guidesr.us:42/the-answer/?foo=bar#anchor' | urlFragment }} |
urlHost
Returns the host name component of a URL.
{{ 'https://michael@guidesr.us:42/the-answer/?foo=bar#anchor' | urlHost }} |
urlPass
Returns the password component of a URL, or null
if the URL doesn’t specify a password.
{{ 'https://michael@guidesr.us:42/the-answer/?foo=bar#anchor' | urlPass }} |
urlPath
Returns the resource path component of a URL, or null
if the URL doesn’t specify a path.
{{ 'https://michael@guidesr.us:42/the-answer/?foo=bar#anchor' | urlPath }} |
urlPort
Returns the port number component of a URL, or null
if the URL doesn’t specify a port.
_(PHP’s parse_url()
method returns an integer for the port number component. Wordsmith, by contrast, returns a string object, to preserve chainability with other methods.)_
{{ 'https://michael@guidesr.us:42/the-answer/?foo=bar#anchor' | urlPort }} |
urlQuery
Returns the query string component of a URL, or null
if the URL doesn’t specify a query.
{{ 'https://michael@guidesr.us:42/the-answer/?foo=bar#anchor' | urlQuery }} |
urlScheme
Returns the protocol component of a URL (e.g. ‘http’, ‘https’).
{{ 'https://michael@guidesr.us:42/the-answer/?foo=bar#anchor' | urlScheme }} |
urlUser
Returns the user component of a URL, or null
if the URL doesn’t specify a user.
{{ 'https://michael@guidesr.us:42/the-answer/?foo=bar#anchor' | urlUser }} |
youtubeId
Returns the YouTube video ID from a URL, or null
if the source string isn’t a YouTube video URL.
{{ 'https://www.youtube.com/watch?v=dQw4w9WgXcQ' | youtubeId }} |