-3

I have some php that return a V4 UUID, and example would be: a935941636384aa0b7560617c641cc2e
I need a way to get this string in a varible in this format:
a9359416-3638-4aa0-b756-0617c641cc2e
8chars - 4chars - 4chars - 13chars
Please help me accomplish this .

Volt
  • 3
  • 5

2 Answers2

0

You can do this:

$str = 'a935941636384aa0b7560617c641cc2e';
$str1 = substr($str, 0, 8);
$str2 = substr($str, 8 ,4);
$str3 = substr($str, 12 ,4);
$str4 = substr($str, 16 ,4);
$str4 = substr($str, 20 ,12);

$final = $str1.'-'.$str2.'-'.$str3.'-'.$str4;
Ahmad Mobaraki
  • 7,426
  • 5
  • 48
  • 69
0

You question is poorly written and the example you gave is wrong. The last part can only have have 12 chars, also, you specified 4 groups, when in fact the string is split in 5.

You can use substr, i.e.:

<?php
$uuid = "a935941636384aa0b7560617c641cc2e";
$part1 = substr($uuid, 0, 8);
$part2 = substr($uuid, 8, 4);
$part3 = substr($uuid, 12, 4);
$part4 = substr($uuid, 16, 4);
$part5 = substr($uuid, 20, 12);

Live Demo

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268