Skip to content

Instantly share code, notes, and snippets.

@Aslam97
Created April 25, 2021 20:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aslam97/ca685cbdd3293b641c19e7634173253c to your computer and use it in GitHub Desktop.
Save Aslam97/ca685cbdd3293b641c19e7634173253c to your computer and use it in GitHub Desktop.
Generate referral code
<?php
use Illuminate\Support\Str;
function generateReferralCode($str) {
// get random number min & max 4 digit
$randomNumber = rand(1000, 9999);
// get the first 4 character and trim whitespace
$strName = trim(substr($str, 0, 4));
// get str length
$strLen = strlen($strName);
// If str length less then 4 add missing x character by random str
$substrName = $strLen < 4 ? $strName . Str::random(4 - $strLen) : $strName;
// make name uppercase and concat with the random number
$referral = strtoupper($substrName) . $randomNumber;
return $referral;
}
@Paulywonder-digitech
Copy link

Hello

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment