-3

I want to generate 34 random letters (capitalized and not) and numbers. Before the generated 34 random alphanumerics there is a word pro_sec_ which is there everytime it generate. And the 25th & 26th is 00

So the generated result will be: (just an example)

pro_sec_Vy6Q67sh6HvP90j7gWnq6SxN00p8Gapu66

Another ex. pro_sec_6B57nTshusbnay6esjabxns800nGvaz2Lov

The pro_sec_ and the 00 there in 25th and 26th are always the same and the rest are just generated.

2 Answers2

1

This should work for you:

<?php
function getToken($n) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomString = '';

    for ($i = 0; $i < $n; $i++) {
        $index = rand(0, strlen($characters) - 1);
        $randomString .= $characters[$index];
    }

    return $randomString;
}

echo 'pro_sec_' . getToken(24) . '00' . getToken(9);
Hassan Al-Jeshi
  • 1,450
  • 2
  • 15
  • 20
1

Performant solution without using any loops or calling the function twice:

$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$split = str_split(substr(str_shuffle($chars), 0, 32), 25);
$res = 'pro_sec_'.$split[0].'00'.$split[1];

echo $res; // pro_sec_c8s5iwyQY0nZoJf73EOkC9vqS00dNLWVKg
B001ᛦ
  • 2,036
  • 6
  • 23
  • 31