Code from PHP to Elixir

Had to deal with this a few weeks back and that SO answer saved me.