This question already has answers here:
PHP random string generator
(68 answers)
Closed 7 years ago.
I want to generate a 6 character long unique key in php in which first 3 should be alphabets and next 3 should be digits.
I know about the uniqid() function but it generates 13 characters long key and also it wont fit in my requirement as I need first 3 characters as alphabets and next 3 as numbers.
Any way in which I can modify uniqid() to fit in my requirements?
I also dont want any collisions because if that happens my whole database will be wasted that is why I can't use rand function because it is very likely that I will get collisions
You could create a manual randomizer like this:
<?php
$alphabet = 'abcdefghijklmnopqrstuvwxyz';
$numbers = '0123456789';
$value = '';
for ($i = 0; $i < 3; $i++) {
$value .= substr($alphabet, rand(0, strlen($alphabet) - 1), 1);
}
for ($i = 0; $i < 3; $i++) {
$value .= substr($numbers, rand(0, strlen($numbers) - 1), 1);
}
The $value variable will then be a string like "axy813" or "nbm449".
<?php
$alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
$numbers = "1234567890";
$randstr = '';
for($i=0; $i < 6; $i++){
if($i<3){
$randstr .= $alphabets[rand(0, strlen($alphabets) - 1)];
} else {
$randstr .= $numbers[rand(0, strlen($numbers) - 1)];
}
}
echo $randstr;
?>
this will do the work for you
Related
I have file which contain one very long line string with only letters. No spaces, no new lines.
Currently I'm trying to create function which take two parameters - how many chars and how many words and creates random words.
Example 3 chars, 4 words - asd, jhH, OPa, BaK.
This is my current function
function randomStringWords($words = 0, $chars = 0) {
$fileRead = fopen("myFile.txt", "r");
if ($fileRead) {
while (($line = fgets($fileRead)) !== false) {
$charactersLength = strlen($line);
$randomString = '';
if ($chars >= 3 && $chars <= 8) {
for ($i = 0; $i < $words; $i++) {
$randomString = $line[rand(0, $charactersLength - 1)];
}
}
}
fclose($fileRead);
}
return $randomString;
}
$myRandomString = randomStringWords(3, 4);
echo $myRandomString;
$myRandomString should return 3 chars 4 words. Instead, returns one single random char.
Update: string example from file
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
If I pass words = 3 and chars = 4 the output should be random 3 words with 4 chars each:
asPd
Plwi
OGfw
If I pass words = 3 and chars = 3 the output should be random 3 words with 3 chars each:
yYd
asd
Ikl
and so on..
You are nearly where you want to be. Just little things and it should work.
$randomString = '';
if ($chars >= 3 && $chars <= 8) {
for ($i = 0; $i < $words; $i++) { // first loop over how many words you need
for ($j = 0; $j < $chars; $j++) { // then we loop over the needed chars
$randomString .= $line[rand(0, $charactersLength - 1)]; // .= to add to the existing string
}
$randomString .= ' '; // add a space after each word, you could also add each word to an array here
}
}
Let's go over my code comments:
You only had one loop for words, not considering the number of wanted chars. Adding a second loop for the chars will fix this
$randomString = ... will redeclare the whole variable, that's why you only got one single char as output. .= will add a string to an existing string, just like += will add a number to an existing number.
Each time after the inner loop (the chars) is complete you need to add a space to it to make it a word (or add it to an array of words).
Tip
The PHP function file_get_contents could be more suitable in your case since the file content is only one line. https://www.php.net/manual/en/function.file-get-contents
This question already has answers here:
Break long string into pieces php
(5 answers)
Closed 3 years ago.
I want to make a program which splits a long number into pieces of 13 digited number such that I can loop for every 13 digits just using php.
$number = 012345678901230123456789123
Should output
0123456789123
0123456789123
And it should be for any large number having the number of digit multiple of 13.It looks about looping and algorithm but I want to make it as short as possible and I have doubts on how to do it. So I am just asking about the main concept.
The most dynamic solution is probably to use array_functions on the string.
So str_split to make it array then chunk it in size 13 and implode the arrays.
$number = "012345678901230123456789123";
$arr = array_chunk(str_split($number), 13);
foreach($arr as &$val){
$val = implode($val);
}
https://3v4l.org/LsNFt
You can create a function where you can use your string and size as parameter and return an array of strings of the desired length:
function splitString($str, $packetSize) {
$output = [];
$size = strlen($str);
for ($i = 0; $i < $size; $i += $packetSize) {
if ($i + $packetSize < $size) {
$output[]= substr($str, $i, $packetSize);
} else {
$output[]=substr($str, $i);
}
}
return $output;
}
This question already has an answer here:
Need help ensuring text shifted 'x' amount of spaces is transformed into letters, not random symbols
(1 answer)
Closed 5 years ago.
$string = "abcfght";
$shift = 3;
$shiftedString = "";
for ($i = 0; $i < strlen($string); $i++)
{
$ascii = ord($string[$i]);
$shiftedChar = chr($ascii-$shift);
$shiftedString .= $shiftedChar;
}
echo $shiftedString;
In the above code b shifted -3 so output is display according to asci table, but my expectation is that output "z"
try this, check the live demo
$string = "abcfght";
$shift = 3;
$chars = str_split($string);
$arr = array_map(function($v){
$shift = ord($v) -3;
echo $shift."\n";
return $shift < 97 ? chr($shift + 26) : chr($shift);
}, $chars);
echo implode('', $arr);
This question already has answers here:
PHP random string generator
(68 answers)
Closed 7 years ago.
I'm trying to create a random string with numbers and letters and I found this function and thought it would be good, but I don't know if it is the correct way to create a true random string or if there is an easier way to do this? Below is what I have:
function randomGen() {
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$length = strlen($chars);
$random;
for ($i = 0; $i < 8; $i++) {
$random = $chars[rand(0, $length - 1)];
}
return $random;
}
You could try using $random = substr(str_shuffle(MD5(microtime())), 0, 8);, which will output the same amount of random characters as you have in your example. I actually prefer this method over most as it doesn't require you to put in the expected characters and even more importantly, it can be done in one line of code!
Hie guys i want to create a random string of numbers where there is a fixed letter B at the beginning and a set of eight integers ending with any random letter, like for example B07224081A where A and the other numbers are random. This string should be unique. How can I do this?
Do you mean something like this?
$letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$numbers = rand(10000000, 99999999);
$prefix = "B";
$sufix = $letters[rand(0, 25)];
$string = $prefix . $numbers . $sufix;
echo $string; // printed "B74099731P" in my case
The more characters - the greater chance to generate unique string.
I think that's much better method to use uniqid() since it's based on miliseconds. Uniqueness of generated string is guaranteed.
This should work for you.
$randomString = "B";
for ($i = 0; $i < 9; $i++) {
if ($i < 8) {
$randomString.=rand(0,9);
}
if ($i == 8) {
$randomString.=chr(rand(65,90));
}
}
echo $randomString;