Random string generator PHP [duplicate] - php

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!

Related

Split a number by 13 digits using php [duplicate]

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;
}

Generate random numbers [duplicate]

This question already has answers here:
Generating UNIQUE Random Numbers within a range
(14 answers)
Closed 7 years ago.
I am trying to find a solution in PHP that can generate three random numbers.
At the moment I have this that generates a random number that is different from $randNum;
The numbers need to be different from each other and also different from the variable $randNum
Thank you
$wrong = $randNum;
while ($wrong == $randNum) {
$wrong = rand(0,$max - 1);
}
<?php
$numbers = [];
for($i = 0; $i < 10; $i++){
$number = rand (1,15);
while (in_array($number, $numbers)){
$number = rand (1,15);
}
$numbers[] = $number;
}
echo '<pre>';
print_r($numbers);
echo '</pre>';
This function generates unique 10 random numbers, range from 1-15 you can easy change this script to your needs.

Generating random string of fixed length [duplicate]

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

Fast way to generate token - PHP [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP: How to generate a random, unique, alphanumeric string?
i want to generate random token [alphanumeric] for random length [between 4-6] characters.
Can anyone help ?
You could use uniqid (search for "token" in the examples given there) and shorten it with substr.
Firstly, you can just get a random number between 10+26+26=62 6 times, and then calculate the resulted string, this seems easy enough.
<?php
function ()
{
$letters={a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9,10}
return array_rand($letters).array_rand($letters)......... // you get the point
?>
or if you prefer the 'hard' way...
$len = random(4,6);
$token = array();
for ($i = 0; $i < $len; $i++) {
$ord = 0;
switch(random(1,3)) {
case 1: // 0 - 9
$ord = random(48,57);
break;
case 2: // A - Z
$ord = random(65,90);
break;
case 3: // a - z
$ord = random(97,112);
break;
}
$token[] = chr($ord);
}

Check if a String Starts with a Number in PHP [duplicate]

This question already has answers here:
Closed 12 years ago.
The community reviewed whether to reopen this question 10 months ago and left it closed:
Original close reason(s) were not resolved
Possible Duplicate:
Check if a String Ends with a Number in PHP
I'm trying to implement the function below. Would it be best to use some type of regex here? I need to capture the number too.
function startsWithNumber($string) {
$startsWithNumber = false;
// Logic
return $startsWithNumber;
}
You can use substr and ctype_digit:
function startsWithNumber($string) {
return strlen($string) > 0 && ctype_digit(substr($string, 0, 1));
}
The additional strlen is just required as ctype_digit returns true for an empty string before PHP 5.1.
Or, if you rather want to use a regular expression:
function startsWithNumber($str) {
return preg_match('/^\d/', $str) === 1;
}
Something like to this may work to you:
function str2int($string) {
$length = strlen($string);
for ($i = 0, $int = ''; $i < $length; $i++) {
if (is_numeric($string[$i]))
$int .= $string[$i];
else break;
}
return (int) $int;
}

Categories