0

I need add - in specific positions of ID, i've tried but only works for numbers and don't work for numbers and letters.

I have this: 93e6276537d438fbb4fdb55415afe13d and i need something like:

93e6276-537d-438f-bb4f-db55415afe13d

Add - after position 8 and after position 12, position 16 and 20.

Many thanks

  • Note: Depending on how you plan on using your UUID, you could just directly parse your current string into binary form using `$bin = pack("h*", $guid);` – Tim Biegeleisen Aug 16 '19 at 02:18
  • @TimBiegeleisen Your dupe gives the java equivalent of my answer, is there one that does it in PHP? – Nick Aug 16 '19 at 02:23
  • @Nick Most likely the OP doesn't even want to be doing this. It is preferable to store a UUID as a byte array in a database, not as a string with hyphens. – Tim Biegeleisen Aug 16 '19 at 02:24
  • Hi have this: ` `, I need tou pass value to new variable, any idea? – user8422931 Aug 16 '19 at 11:02

1 Answers1

1

You can use preg_replace to split the string based on the lengths of the segments:

echo preg_replace('/^(.{8})(.{4})(.{4})(.{4})(.*)$/', '$1-$2-$3-$4-$5', '93e6276537d438fbb4fdb55415afe13d');

Output:

93e62765-37d4-38fb-b4fd-b55415afe13d

Demo on 3v4l.org

Nick
  • 138,499
  • 22
  • 57
  • 95