Steam ID to Steam community ID

The following PHP code converts a Steam ID (STEAM_X:Y:Z) into a steam community ID. It has been sitting on my desktop for a while now, and I need to do something with it.

<?php
function steamID2CommunityID($steam_id) {
    $steam_id = preg_replace("/^STEAM_/i", "", $steam_id);
    if (preg_match("/^0:([0-1]):([0-9]+)$/", $steam_id, $m))  {
        $community_id = $m[2];
        $community_id = bcmul($community_id, 2);
        $community_id = bcadd($community_id, "76561197960265728");
        $community_id = bcadd($community_id, $m[1]);
        return $community_id;
    } else {
        throw new Exception("Invalid Steam ID not of format 0:[0-1]:[0-9]+");
    }
}

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>