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);
}
Related
This question already has answers here:
Get the sum of all digits in a numeric string
(13 answers)
Closed 8 months ago.
I'm trying to adding all numbers from last 6 digit from substr(). Let say the number is 19283774616, I'm trying to have result from this: 7+7+4+6+1+6 = ?. Here is my current code
public function accountHash($accountNumber)
{
$result = 0;
$accountNumber = substr($accountNumber, -6);
for($i=0; $i<=strlen($accountNumber); $i++) {
$result += substr($accountNumber, $i, 1); // A non-numeric value encountered here
}
echo $result;
}
From the function above, "A non-numeric value encountered" error occurred. Need suggestion on how to do this. Thank you
You attempt to get more characters than string contains. Replace "<=" with "<" in your condition expression, i.e. change:
for($i=0; $i<=strlen($accountNumber); $i++) {
to
for($i=0; $i<strlen($accountNumber); $i++) {
You need to use < instead of <= in your for loop.
And you can do it a more simple way,
$result = 0;
for($i = 0; $i < 6; $i++){
$result += $string[-$i];
}
An alternative method without loops (or error checking, for what it's worth):
function accountHash($accountNumber)
{
return array_sum(
preg_split('//u', mb_substr($accountNumber, -6), null, PREG_SPLIT_NO_EMPTY)
);
}
Demo
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:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
one of the rules in our password creation is, it shouldn't contain a sequence of number or alphabets.
ex.12345, pwd45678, pwd_abcdef, pwd_abc123
all of these are not allowed.
Any suggestion how to check for sequence?
By sequence meaning it shouldn't be order like for numbers 1-10 or letters in the alphabet. So the password shouldn't contain alphabet sequence or order and numbers in 1-10 order. So password containing ABC or DEF or HIJK is not allowed and passwords containing number orders like 1234 or 4567 are not allowed but passwords containing ABNOE or 19334 is ok.
TIA
A specific rule for no 2 adjacent digits or letters:
if (preg_match("#(\d{2,}|[[:alpha:]]{2,})#u", $input)) {
return false;
}
You can try it out here.
However, there are packages available specifically for password strength checking. They will have configurable rules or tests.
you can use the code below,I used the "asci code" to resolve the problem, it is already tested for your examples :
<?php
$passwords = [
'12345',
'pwd45678',
'pwd_abcdef',
'pwd_abc123',
];
var_dump(check_password_sequence($passwords[3], 4));
function check_password_sequence($password, $max) {
$j = 0;
$lenght = strlen($password);
for($i = 0; $i < $lenght; $i++) {
if(isset($password[$i+1]) && ord($password[$i]) + 1 === ord($password[$i+1])) {
$j++;
} else {
$j = 0;
}
if($j === $max) {
return true;
}
}
return false;
}
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!
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;
}