3

I am trying to place a dash at certain intervals in a string in PHP. Here is the string I have, retrieved from a JSON GET:

fb396a80cada446f9e63d871116b8ddf

This is what I need the string to look like:

fb396a80-cada-446f-9e63-d871116b8ddf

How would I place the dashes at the intervals?

I've tried this, which didn't work:

$mcuuid = $json_decode->id;

$p1 = substr($mcuuid, 0, 7);
$p2 = substr($mcuuid, 8, 12);
$p3 = substr($mcuuid, 12, 15);
$p4 = substr($mcuuid, 16, 19);
$p5 = substr($mcuuid, 20, 31);

$final = $p1 . "-" . $p2 . "-" . $p3 . "-" . $p4 . "-" . $p5;
spenibus
  • 4,339
  • 11
  • 26
  • 35
Joosh
  • 136
  • 1
  • 9

2 Answers2

3
            $mcuuid = "fb396a80cada446f9e63d871116b8ddf" ;
            $p1 = substr($mcuuid, 0, 7);
            $p2 = substr($mcuuid, 8, 4);
            $p3 = substr($mcuuid, 12, 4);
            $p4 = substr($mcuuid, 16, 4);
            $p5 = substr($mcuuid, 20);


            $final = $p1 . "-" . $p2 . "-" . $p3 . "-" . $p4 . "-" . $p5;

            echo $final;
Lucky Chingi
  • 2,248
  • 1
  • 10
  • 15
3

You could do it with a regex.

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

Output:

fb396a80-cada-446f-9e63-d871

. is any character
{} is the number of characters the preceding character/group you want to allow. () captures the value found inside it
$1 in the replace is the 1 captured group
* is a quantifier meaning zero or more characters
^$ are anchors; they require the full string is a match (more specifically ^ is the start of the string and $ is the end).

Regex101 demo: https://regex101.com/r/oI1hU0/1

chris85
  • 23,846
  • 7
  • 34
  • 51
  • sure hoping we'll see a green tick here, or in his other questions. Probably he doesn't know how Stack rolls around here. *"A little something for your trouble"* ;-) – Funk Forty Niner Nov 01 '15 at 02:47
  • @Fred-ii- Yea, noticed that after you posted. As a moderator (or user with moderator privileges) does it show you that data easier or do you have to open their profile as well? Hopefully OP will view your provided link and learn how SO works. – chris85 Nov 01 '15 at 02:54
  • 1
    I just open their profile and see all his questions. Most of which holding answers that most likely solved them and no green tick. I thought it was time that he was told how Stack rolls ;-) However, I'm not a moderator. Many want me to be, but it's a bigger "job" than most people think it is. It would be nice to be granted some type of "extra power" ;-) Low rep'ed members hate me, while the mods "tolerate" me, and some higher rep'd lol but am sure they appreciate the extra work I do around here. – Funk Forty Niner Nov 01 '15 at 02:56
  • this worked thanks, and i learned somthing new :p. Whats this green tick that i keep missing? – Joosh Nov 01 '15 at 02:59
  • 1
    The green tick/check mark, indicates that the answer resolved your issue. You should use it after an answer has resolved your question. This indicates to users trying to help you that the issue has been resolved, and to future users that the answer worked to resolve the question they are viewing (so presumably they may be able to use checked answer if their question were the same). Take a look at the link from Fred, that gives a lot more information. – chris85 Nov 01 '15 at 03:02
  • 1
    @JoshAllport Actually, Chris himself said so [in a comment here](http://stackoverflow.com/questions/33331430/php-list-users-from-sql-database-in-table/33331610#comment54460154_33331430) for an answer I gave you ;-) You may not have seen it, but it's there. You should also note and pay attention to comments given in questions. Many times, comments are posted in order to get more information about something that may not be clear. You can't always rely on answers. Many a time, comments also "answer/solve" problems. – Funk Forty Niner Nov 01 '15 at 03:08
  • ahh, alrioght thank you i will pay attention to it more. Im new to this whole thing if you cant tell. – Joosh Nov 01 '15 at 03:11
  • @JoshAllport You're welcome Josh. We're always happy to help, *cheers*. Plus, you also get reputation points too. – Funk Forty Niner Nov 01 '15 at 03:13