Skip to content

Instantly share code, notes, and snippets.

@uniqueginun
Created June 12, 2024 18:41
Show Gist options
  • Save uniqueginun/d31492ca2a84f9608cdb718ff906854d to your computer and use it in GitHub Desktop.
Save uniqueginun/d31492ca2a84f9608cdb718ff906854d to your computer and use it in GitHub Desktop.
extract uuid from string - PHP
function extractUuid(string $string) {
$uuidPattern = '/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i';
$uuid = preg_match($uuidPattern, $string, $matches) ? $matches[0] : null;
return $uuid;
}
echo extractUuid("some other string 42344k-23423-dfs34-9--2342"); // null
echo extractUuid("something 4e8e39e8-5856-424d-a0e5-d994416aebda"); // 4e8e39e8-5856-424d-a0e5-d994416aebda
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment