Skip to content

Instantly share code, notes, and snippets.

@matheuseduardo
Last active September 6, 2025 04:43
Show Gist options
  • Save matheuseduardo/d9b37df0166f0119573a46e1ab843bfe to your computer and use it in GitHub Desktop.
Save matheuseduardo/d9b37df0166f0119573a46e1ab843bfe to your computer and use it in GitHub Desktop.
função para converter steam ids
<?php
class SteamIDConverter
{
private const V = "76561197960265728"; // Offset para contas individuais
/**
* Converte entre formatos de SteamID (STEAM_X:Y:Z, [U:1:W], SteamID64).
*
* @param string $input O valor do SteamID a ser convertido
* @param int $type Tipo de SteamID (0 = steamID, 1 = steam3ID, 2 = steamID64)
* @return array|bool Retorna um array com 'steamID', 'steam3ID' e 'steamID64' ou false em caso de erro
*/
public static function convert($input, $type)
{
$W = null;
$Y = null;
$Z = null;
$universe = 1; // padrão (Public)
switch ($type) {
case 0: // SteamID (STEAM_X:Y:Z)
$parsed = self::parseSteamID($input);
if ($parsed === false) {
return false;
}
$universe = $parsed['X'];
$Y = $parsed['Y'];
$Z = $parsed['Z'];
$W = $Z * 2 + $Y;
break;
case 1: // Steam3 ID ([U:1:W])
$W = self::parseSteam3ID($input);
if ($W === false) {
return false;
}
$Y = $W % 2;
$Z = ($W - $Y) / 2;
break;
case 2: // SteamID64
$steamID64 = self::validateSteamID64($input);
if ($steamID64 === false) {
return false;
}
$W = bcsub($steamID64, self::V);
$Y = bcmod($W, "2");
$Z = bcdiv(bcsub($W, $Y), "2");
break;
default:
return false;
}
$steamID = "STEAM_{$universe}:{$Y}:{$Z}";
$steam3ID = "[U:1:{$W}]";
$steamID64 = bcadd(bcadd(bcmul($Z, "2"), $Y), self::V);
return [
'steamID' => $steamID,
'steam3ID' => $steam3ID,
'steamID64' => $steamID64
];
}
// --- Funções auxiliares privadas ---
private static function parseSteamID($input)
{
if (!preg_match('/^STEAM_([0-5]):([0-1]):(\d+)$/', $input, $matches)) {
return false;
}
return [
'X' => (int)$matches[1],
'Y' => (int)$matches[2],
'Z' => (int)$matches[3]
];
}
private static function parseSteam3ID($input)
{
if (!preg_match('/^\[U:1:(\d+)\]$/', $input, $matches)) {
return false;
}
return (int)$matches[1];
}
private static function validateSteamID64($input)
{
if (!ctype_digit($input) || bccomp($input, self::V) < 0) {
return false;
}
return $input;
}
}
<?php
$inputs = [
['value' => 'STEAM_0:0:12574760', 'type' => 0],
['value' => '[U:1:25149520]', 'type' => 1],
['value' => '76561197985415248', 'type' => 2]
];
foreach ($inputs as $input) {
$result = SteamIDConverter::convert($input['value'], $input['type']);
if ($result) {
echo "Entrada: {$input['value']} (Tipo: {$input['type']})\n";
echo "SteamID: {$result['steamID']}\n";
echo "Steam3 ID: {$result['steam3ID']}\n";
echo "SteamID64: {$result['steamID64']}\n\n";
} else {
echo "Erro: Entrada inválida para {$input['value']} (Tipo: {$input['type']})\n\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment