0

I have GUIDs in this format

2k9E4A8DFpFFYsfNkKz5fnx61Ry2xAoO

another example:

MI5kvu_WGg3MLB4l18UG4oJ63a1H8uF_

but I need them in this format:

4a9209bd-9252-4914-01a3-24c283062394

Is there a way to convert them in PHP? How would you convert them back (from UUID to GUID)?

David Barratt
  • 546
  • 1
  • 6
  • 24
  • 1
    they're just strings. just because they're guids doesn't make them magically different. use [string functions](http://php.net/manual/en/ref.strings.php). – Marc B Jul 22 '16 at 16:39
  • @MarcB the byte order is different https://en.wikipedia.org/wiki/Globally_unique_identifier#Binary_encoding so they are not just a string. – David Barratt Jul 22 '16 at 16:58
  • what byte order? `oO` isn't exactly a valid hex number. What OP has is a string. – Marc B Jul 22 '16 at 16:59
  • I see. Here's another example of a GUID I have... `MI5kvu_WGg3MLB4l18UG4oJ63a1H8uF_` and it doesn't look like underscores are part of UUID. :( – David Barratt Jul 22 '16 at 17:01
  • 1
    http://stackoverflow.com/questions/246930/is-there-any-difference-between-a-guid-and-a-uuid –  Jul 22 '16 at 17:12
  • @0x13a is it valid to have underscores in UUID/GUID or are the GUIDs I have invalid? – David Barratt Jul 22 '16 at 17:15
  • Looks like a UUID can only contain hexadecimal characters, but both GUID examples I've given have non-hexadecimal characters. – David Barratt Jul 23 '16 at 17:01
  • What system/software generated the GUID examples you've given? Most documentation I've seen describes UUIDs and GUIDs as being nearly interchangeable. Indeed, Microsoft GUIDs these days are pretty much RFC 4122 UUIDs. UUIDs must be hexadecimal, so if you have something generating GUIDs with different characters, I can either assume they aren't real GUIDs, or they are using a different base (other than base-16, hex) to pack them into a smaller format. – ramsey Mar 01 '17 at 21:59
  • They are from https://www.comcasttechnologysolutions.com/ I found out that the GUID they are referring to is not a standard GUID and could be any string. – David Barratt Mar 02 '17 at 21:14

1 Answers1

-1

Quick n dirty function to take your original and fudge it into required pattern

$guid='2k9E4A8DFpFFYsfNkKz5fnx61Ry2xAoO';

function makeuuid($g){
    $a=array(
        substr($g,0,8),
        substr($g,8,4),
        substr($g,12,4),
        substr($g,16,4),
        substr($g,20,strlen($g))
    );
    return implode('-',$a);
}
echo makeuuid( $guid );

/* outputs: 2k9E4A8D-FpFF-YsfN-kKz5-fnx61Ry2xAoO */
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • [In its canonical form, a UUID is represented by 32 lowercase hexadecimal digits](https://en.wikipedia.org/wiki/Universally_unique_identifier#Definition) – Finwe Jul 22 '16 at 16:50
  • So if we were to use a `strtolower()` in the above code, would that resolve the issue and get the proper result? – David Barratt Jul 22 '16 at 16:55
  • 1
    Oh I see, because the GUID has non-hexadecimal characters, this script does not work. – David Barratt Jul 23 '16 at 17:02