Similar to this answer which formats digital strings to hyphenated phone numbers, you can sensibly use preg_replace()
or a combination of sscanf()
with a joining function.
Code: (Demo) (or no character classes)
$UUID = "f9e113324bd449809b98b0925eac3141";
echo preg_replace('/(?:^[\da-f]{4})?[\da-f]{4}\K/', '-', $UUID, 4);
echo "\n---\n";
vprintf('%s-%s-%s-%s-%s', sscanf($UUID, '%8s%4s%4s%4s%12s'));
echo "\n---\n";
echo implode('-', sscanf($UUID, '%8s%4s%4s%4s%12s'));
Output:
f9e11332-4bd4-4980-9b98-b0925eac3141
---
f9e11332-4bd4-4980-9b98-b0925eac3141
---
f9e11332-4bd4-4980-9b98-b0925eac3141
\w
is already case-insensitive. – Geulincx