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 .
Asked
Active
Viewed 187 times
-3

Volt
- 3
- 5
-
There's a `substr` function, use it. – u_mulder Dec 25 '16 at 21:01
-
http://stackoverflow.com/questions/2040240/php-function-to-generate-v4-uuid – rob2universe Dec 26 '16 at 02:32
2 Answers
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
-
You may want to test it again. How many chars do you have on first part ? – Pedro Lobito Dec 25 '16 at 21:21
-
-
Still wrong, read the `substr` manual. The 1st arg of `substr` is the char # to start and the 2nd is the # of chars to match - http://php.net/manual/en/function.substr.php – Pedro Lobito Dec 25 '16 at 21:26
-
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);

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