2

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 $json['id'] 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.

LeoV117
  • 114
  • 12
  • You haven't specified what language you're trying to achieve the formatting of UUID's in... You've mentioned the word Java while only supplying PHP code and queries. Can you be a little more clear about what you're trying to do? – px06 Sep 15 '16 at 13:40
  • Done as requested. Updated in title and question. – LeoV117 Sep 15 '16 at 13:43

2 Answers2

2

UUID is not a special java format. It is the Universal Unique Identifier.

A universally unique identifier (UUID) is an identifier standard used in software construction. A UUID is simply a 128-bit value.

What happen is that the conversion from the 128 bit value to a human readable version of the same value converting it to a string follow generally some conventions.

Basically the number is converted to the better human readable hexadecimal format with some hyphen to separate block of bits.


From a performance perspective you can use the several times the substr_replace function, instead of creating an array of strings using substr and applying implode to it.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
  • Review Question: _[...] 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._ – LeoV117 Sep 15 '16 at 13:46
2

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(                                            
                        "(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})",                            
                        "$1-$2-$3-$4-$5");                                                      

        UUID uuid = UUID.fromString(uuid_parse);
        System.out.println(uuid);
    }
}

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

Or in PHP you can do something like:

<?php 
    $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: https://stackoverflow.com/a/33484855/4195825

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

Community
  • 1
  • 1
px06
  • 2,256
  • 1
  • 27
  • 47
  • Your second answer directed me to an [effective solution](http://stackoverflow.com/a/33484916/3257339) through the following means: `$uuid = preg_replace( "/(\w{8})(\w{4})(\w{4})(\w{4})(\w{12})/i", "$1-$2-$3-$4-$5", $json[ 'id' ] );` – LeoV117 Sep 15 '16 at 14:08
  • @LeoV117 while I think that regex is nice to use, in some cases I tend to avoid it when I can see a solution with native features like `substr()` however, in this case regex **may** have an advantage of being faster. – px06 Sep 15 '16 at 14:10