March 26, 2012

MediaWiki Template Get Hostname

I was working on a template for one my personal wikis and needed to get the hostname for a given URL. Using the capabilities of the Parser Functions extension for MediaWiki I whipped up this template. I figured others may find this useful so here it is. The first version has a bunch of spaces and newlines added to make it more readable.

{{#vardefine: hoststart | {{#expr: {{#pos: {{{1|}}} | // }} + 2 }} }}
{{#vardefine: hostend | {{#pos: {{{1|}}} | / | {{#expr: {{#pos: {{{1|}}} | // }} + 2 }} }} }}
{{#vardefine: hostlen | {{#expr: {{#var: hostend }} - {{#var: hoststart }} }} }}
{{#sub: {{{1|}}} | {{#var: hoststart}} | {{#var: hostlen}} }}

To put it in your own MediaWiki, copy this version that removes the spaces and newlines.

To use this template put it on a page like Template:Get hostname and then call it in your pages as

{{Get hostname|http://thingelstad.com/another-reason-you-need-to-use-a-password-manager/}}

which will return thingelstad.com.


MediaWiki Techie


Previous post
Green Hat This morning Tyler and I made a donut run for the family. He was digging his green hat.
Next post
Travels to Iceland My friend Dennys Bisogno just self-published a photo book on a trip that he and my other friend Steve LeVahn took to Iceland. It’s a free book in