Created
April 25, 2021 20:49
-
-
Save Aslam97/ca685cbdd3293b641c19e7634173253c to your computer and use it in GitHub Desktop.
Generate referral code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello