Search code examples
phphexsubstrstring-lengthunique-id

How can I change the length of a hex number?


I have a function who generates a Hex number:

function generateUid(){
    $uuid = Uuid::uuid4();
    $uuid = $uuid->getHex();
    $uuid =  substr($uuid,8);

    return   $uuid;
}

The hex has the length of 25 but I want to cut it to only 8 digits.


Solution

  • The substr first parameter is where to start, and second is the length (number of characters to return). So:

    substr($uuid, 0, 8);
    

    Should start at the first position and return 8 characters.

    Your previous code:

    substr($uuid,8);
    

    started at the 9th character and returned the rest of the string.