PHP Add dash to decode

2020-07-20 03:46发布

If i have the uuid now it will come out like this 42f704ab4ae141c78c185558f9447748 But is it possible to let it string in this 42f704ab-4ae1-41c7-8c18-5558f9447748 so with dashes on the certain places

<?php 
$playername = 'Vanture';
$url = "https://api.mojang.com/users/profiles/minecraft/" . urlencode($playername);
$result = file_get_contents($url);
$json = json_decode($result);
if ($json == NULL) {
    echo("NULL returned - probably invalid playername");
} else {
    $UUID = $json->id;
    echo "$UUID $playername";
}
?>

标签: php json string
2条回答
▲ chillily
2楼-- · 2020-07-20 04:18
<?php 

//$UUID = 42f704ab-4ae1-41c7-8c18-5558f944774
$UUID = "42f704ab4ae141c78c185558f9447748";


$UUID = substr($UUID, 0, 8) . '-' . substr($UUID, 8, 4) . '-' . substr($UUID, 12, 4) . '-' . substr($UUID, 16, 4)  . '-' . substr($UUID, 20);
echo $UUID;
查看更多
干净又极端
3楼-- · 2020-07-20 04:31

Here's another way:

$input = "42f704ab4ae141c78c185558f9447748";
$uuid = preg_replace("/(\w{8})(\w{4})(\w{4})(\w{4})(\w{12})/i", "$1-$2-$3-$4-$5", $input);
echo $uuid;
查看更多
登录 后发表回答