LeoV117 LeoV117 - 8 months ago 51
PHP Question

Format "raw" string to Java UUID in PHP

Is there a more effective, simplified means of converting a "formatted" Java UUID - without dashes - to a Java compatible format - with dashes - in PHP, and ultimately: How would I do it?

I have code that already performs this action, but it seems unprofessional and I feel that it could probably be done more effectively.

[... PHP code ...]
$uuido = $json['id'];

$uuidx = array();
$uuidx[0] = substr( $uuido, 0, 8 );
$uuidx[1] = substr( $uuido, 8, 4 );
$uuidx[2] = substr( $uuido, 12, 4);
$uuidx[3] = substr( $uuido, 16, 4);
$uuidx[4] = substr( $uuido, 20, 12);

$uuid = implode( "-", $uuidx );
[... PHP code ...]

Input: f9e113324bd449809b98b0925eac3141
Output: f9e11332-4bd4-4980-9b98-b0925eac3141

The data from
is called from the following Mojang Profile API using the
file_get_contents( $url )
function combined with a
json_decode( $file )
, which could alternatively be done through cURL - but since it would eventually be requesting anything up to 2048 profiles at once, I figured it would become slow.

I do have my code in use, and public, through the following ProjectRogue Server Ping API, which usually contains a list of online players.

Note: There are several questions related to this, but none apply to PHP as far as I am aware. I have looked.

I mention Java UUID as the parsed output should effectively translate to a Player UUID for use in a Java-Based plugin for Spigot or Craftbukkit after 1.7.X.


Your question doesn't make much sense but assuming you want to read the UUID in the correct format in Java you can do something like this:

import java.util.UUID;
class A
    public static void main(String[] args){

        String input = "f9e113324bd449809b98b0925eac3141";                         
        String uuid_parse = input.replaceAll(                                            

        UUID uuid = UUID.fromString(uuid_parse);

Borrowed from maerics, see here: http://stackoverflow.com/a/18987428/4195825

Or in PHP you can do something like:

    $UUID = "f9e113324bd449809b98b0925eac3141";
    $UUID = substr($UUID, 0, 8) . '-' . substr($UUID, 8, 4) . '-' . substr($UUID, 12, 4) . '-' . substr($UUID, 16, 4)  . '-' . substr($UUID, 20);
    echo $UUID;

Borrowed from fico7489: http://stackoverflow.com/a/33484855/4195825

And then you can send that to Java where you can create a UUID object using fromtString().