How to get string of fixed length without using rand() function - php

How to get a fixed length string without using the rand() function?
I have this but I do not want to use the rand() function
function generateRandomString($length =6) {
$characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($j = 0; $j < $length; $j++) {
$randomString .= $characters[mt_rand(0, $charactersLength - 1)];
}
return $randomString;
}

<?php
$start=0;$length=6;
$str='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$Val=substr(str_shuffle($str),$start,$length);
echo $Val;
?>

function mt_rand_str ($l, $c = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890') {
for ($s = '', $cl = strlen($c)-1, $i = 0; $i < $l; $s .= $c[mt_rand(0, $cl)], ++$i);
return $s;
}
Here you can call this function for 8 character
mt_rand_str(8)

you can use following code to get fixed length random string
<?php
$str = 'abcdef';
$shuffled = str_shuffle($str);
// this will genrate randome string with fixed string lenght
echo $shuffled;
?>

Related

PHP Unique random string generator

How to make a random string unique to the string in the column below?
<?php
$n=10;
function getName($n) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $n; $i++) {
$index = rand(0, strlen($characters) - 1);
$randomString .= $characters[$index];
}
return $randomString;
}
echo getName($n);
?>
<?php
$n = 10;
function getName($n) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $n; $i++) {
$index = rand(0, strlen($characters) - 1);
$randomString .= $characters[$index];
}
return $randomString;
}
function getUniqueString($length, $dbConnection){
$string = getName($length);
$count = $dbConnection->query("SELECT * FROM TABLE_NAME WHERE string='$string')")->num_rows;
while($count != 0){
$string = getName($length);
$count = $dbConnection->query("SELECT * FROM TABLE_NAME WHERE string='$string')")->num_rows;
}
return $string;
}
echo getUniqueString($n, $dbConnection);
?>
You can use openssl_random_pseudo_bytes() or random_bytes() PHP fonction to generate random strings.
You can also rely on MySQL to generate uniq IDs :
INSERT INTO mytable (alphaID) VALUES (REPLACE( UUID(), '-', '' ));

How to generate unique Voucher code in laravel 5.2?

I want to save unique voucher code and mix of characters and numerics and it should be 6 in length. I am using Laravel Framework 5.2
enter code here
$data = $request->all();
unset($data['_token']);
//echo "<pre>"; print_r($data); die;
for ($i=1; $i <=$data['countvoucher']; $i++) {
$voucher = new Voucher;
$voucher->code = "123456";// it should be dynamic and unique
$voucher->percentage = $data['percentage'];
$voucher->usage = $data['usage'];
$voucher->expirydate = $data['expirydate'];
$voucher->save();
}
$voucher->code i want to save in this filed can anyone help me
I am using this function
You may use it like something like bellow
$voucher->code = $this->generateRandomString(6);// it should be dynamic and unique
public function generateRandomString($length = 20) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
private static function generateNumber()
{
$number = Str::random(9);
if (self::where('number', $number)->count() > 0) self::generateNumber();
return $number;
}
You can use Laravel's built-in helper method str_random which generate a more truly "random" alpha-numeric string as:
str_random(6)
The str_random function generates a random string of the specified length. This function uses PHP's random_bytes function.
So your final code will be as:
$voucher->code = str_random(6);
Here is this code
$chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$code = "";
for ($i = 0; $i < 6; $i++) {
$code .= $chars[mt_rand(0, strlen($chars)-1)];
}
Replace your this line in your code with
$voucher->code = $code;// it should be dynamic and unique
I hope this will work
EDIT
You can try other ways too
$code = strtoupper(uniqid()); // if you dont have any restriction on length of code
For length constraint try this
function generateCouponCode($length = 6) {
$chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$ret = '';
for($i = 0; $i < $length; ++$i) {
$random = str_shuffle($chars);
$ret .= $random[0];
}
return $ret;
}
use Illuminate\Support\Str;
$random = Str::random(6);
"emPK39"

How do I generate a list of random strings and hashes and output it in an HTML table?

I'm trying to generate a list of, let's say 10, random strings and it's SHA-256 hash in an HTML table.
I have found the following code submitted by another user to generate the random string:
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
How do I loop the function and how do I output the string and the hash of the string in an HTML table?
Try this code:
<?php
function generate($length)
{
$string = '';
if($length > 128)
{
$string .= generate($length-128);
}
else
{
$string = substr(hash('sha512',mt_rand()),0,$length);
}
return $string;
}
$table = '<table>%s</table>';
$item = '<tr><td>%s</td><td>%s</td></tr>';
$res = '';
for($i=0;$i<10;$i++)
{
$str = generate(10);
$hash = hash('sha256', $str);
$res .= sprintf($item, $hash, $str);
}
echo sprintf($table, $res);

How to generate unique string which in not into specific array?

I want to generate unique string.
My code is:
function string(){
$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$str= '';
for ($i = 0; $i < 3; $i++) {
$str.= $characters[rand(0, strlen($characters) - 1)];
}
return $str;
}
above code through I'm Generate unique string but that string must not in below array
$array = array('adc','Fs5','sf9','9Sf', ..........);
Anyone know how to do this?
$array = array('adc','Fs5','sf9','9Sf', ..........);
function string() {
global $array;
$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
do {
$str= '';
for ($i = 0; $i < 3; $i++) {
$str.= $characters[rand(0, strlen($characters) - 1)];
}
} while (in_array($str, $array));
return $str;
}

Want to generate random number including quotes

I want to genetate a random number like a,b,c,'and ".So far i have tried
<?php
function generateRandomString($length = 9) {
$char = 'abcd';
$randomString = '';
for ($i = 0; $i <$length; $i++) {
$randomString = $char[rand(0, strlen($char) - 1)];
}
return $randomString;
}
$ran=generateRandomString();
?>
It generates a,b,c,d as random string
But if i try " like $char='abcd"'; then it generates q,u,o,a,& etc.
<?php
function generateRandomString($length = 9) {
$char = 'abcd"\'';
$randomString = '';
for ($i = 0; $i <$length; $i++) {
$randomString = $char[rand(0, strlen($char) - 1)];
}
return $randomString;
}
$ran=generateRandomString();
?>
Here is solution:
$char = 'abcd"\'';
You can escape and pass the quote.
$char = 'abcd\''

Categories