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\''
Related
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(), '-', '' ));
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 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;
?>
I need to generate random password with condition:
All characters are allowable except "l1o0"
Length of 8 to 12 in characters
Codes I've tried:
function generateRandomPassword() {
//Initialize the random password
$password = '';
//Initialize a random desired length
$desired_length = rand(8, 12);
for($length = 0; $length < $desired_length; $length++) {
//Append a random ASCII character (including symbols)
$password .= chr(rand(32, 126));
}
return $password;
}
How to avoid these 4 characters => "l1o0" ?
Reason:
These 4 characters are sometimes confused the user.
Thanks!
Please don't use any of the other answers currently provided for generating passwords. They're not secure by any measure.
rand() -> No
mt_rand() -> Definitely not
I'm going to pull this solution from a blog post aptly titled How to Securely Generate Random Strings and Integers in PHP.
/**
* Note: See https://paragonie.com/b/JvICXzh_jhLyt4y3 for an alternative implementation
*/
function random_string($length = 26, $alphabet = 'abcdefghijklmnopqrstuvwxyz234567')
{
if ($length < 1) {
throw new InvalidArgumentException('Length must be a positive integer');
}
$str = '';
$alphamax = strlen($alphabet) - 1;
if ($alphamax < 1) {
throw new InvalidArgumentException('Invalid alphabet');
}
for ($i = 0; $i < $length; ++$i) {
$str .= $alphabet[random_int(0, $alphamax)];
}
return $str;
}
Usage:
// Every ASCII alphanumeric except "loIO01":
$alphabet = 'abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
$string = random_string(12, $alphabet);
You probably don't have random_int(), unless you're reading this in the future when PHP 7 is released. For those of us living in the present, use random_compat.
Try this:
function generateRandomPassword($length = 8) {
$characters = '23456789abcdefghjklmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomPassword = '';
for ($i = 0; $i < $length; $i++) {
$randomPassword .= $characters[rand(0, $charactersLength - 1)];
}
return $randomPassword;
}
You don't need to change your code . just use str_replace to replace those word's You can try this solution :) . Just edited your code
function generateRandomPassword($length = 8) {
$characters = '23456789abcdefghjklmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomPassword = '';
for ($i = 0; $i < $length; $i++) {
$randomPassword .= $characters[rand(0, $charactersLength - 1)];
}
return str_replace(['l','1','o','0'], ['A','B','C','D'], $randomPassword);
}
$string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$req_pword_len = 20;
$char_count = 0;
$password='';
$chars=str_split($string);
while ( $char_count < $req_pword_len ) {
$char = mt_rand(0,61);
$password .= (string) $chars[$char];
$char_count++;
}
Change the value for
$string to be just the characters you want to allow
$req_pword_len to the required length of the password
Try this:
function generateRandomPassword($length = 8) {
$randomPassword = '';
for ($i = 0; $i < $length; $i++) {
while (true) {
//remove 0,1,I,O,l,o
while(in_array(($number = rand(65, 122)), array(48, 49, 73, 79, 108, 111)));
if ($number <= 90 or $number >= 97) {
$randomPassword .= chr($number);
break ;
}
}
}
return $randomPassword;
}
echo generateRandomPassword();
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;
}