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;
}
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
I'm trying to prevent non numbers from being inserted into my database, preferably not exceeding 6 digits. Currently my regex is failing to match and exit out of the PHP script if non-numeric characters are entered, and consequently, the data gets inserted into the database. I cannot figure out why, as its stated everywhere online that this solution should be correct. The data that's coming through is an array of characters, formed from an exploded string of those characters if that helps.
PHP:
for ($i = 0 ; $i <= $count ; ++$i)
{
if (!preg_match("/^\d+$/", $number_array[$i]))
{
exit();
}
else
{....
Without the loop, get all of the values that are numbers between 1 and 6 digits from the array into another array and then compare with the original array:
if(preg_grep('/^\d{1,6}$/', $number_array) != $number_array) {
exit;
} else {
//something
}
It's a bit longer, but you could also filter out the values that are not numbers between 1 and 6 digits and compare with the original:
(array_filter($number_array, function($v) {
return ctype_digit($v) && (strlen($v) < 7);
}) != $number_array)
This should be the regex you want, it matches all strings of numbers between 1 and 6 digits.
for ($i = 0 ; $i <= $count ; ++$i)
{
if (!preg_match("/^\d{1,6}$/m", $number_array[$i]))
{
exit();
}
else
{....
So 156546 will match but these won't.
1565467
156546s
s15654
156546sa
asdfasdfa
I want to solve this problem of Possible three letter words
Here is following words. (17 characters)
19920620forestJSR
How many possible ways to make 3 length word with given characters?
Ex: 192, 162, Rer, ….
Rule:
Number 0 is different alpha o. (0 != o)
case-sensitive is available. (R != r)
same character repeat not available. (rr1 : wrong)
Hint:
17 16 15 : wrong
How can I solve this
I am trying with this code
function permute($str,$i,$n) {
if ($i == $n)
print "$str\n";
else {
for ($j = $i; $j < $n; $j++) {
swap($str,$i,$j);
permute($str, $i+1, $n);
swap($str,$i,$j); // backtrack.
}
}
}
function swap(&$str,$i,$j) {
$temp = $str[$i];
$str[$i] = $str[$j];
$str[$j] = $temp;
}
$str='19920620forestJSR';
permute($str,0,strlen($str));
But in output have some error with numeric characters
Output look like (when str=19920620forestJSR)
n���Z��뢿�Yh��fzj+�ȳz��ߍ�シo+^��aj�-y�k��m��e�ƭ{�6�ټ�zȧo�h���j���Z�ǫ���������z�a���X�y�����
Output look like (when str=forestJSR)
foresSJtR
foresSJRt
foresStJR
foresStRJ
foresSRtJ
foresSRJt
foresRJSt
foresRJtS
The rules says you need combinations of 3 characters, whithout repeating letters. Your code is generating all combinations for 17 letters. So, the script is looping for millions of possibilities (355,687,428,096,000).
The number of permutations of n distinct objects, taken r at a time is
nPr = n! / (n - r)!
So, for "19920620forestJSR" (14 different letters only), using 3 at a time:
14P3 = 14! / (14 - 3)! = 14! / 11! = (14)(13)(12) = 2184
I am trying to generate random voucher code applying the following rules:
Alphanumeric combination 5 characters in capital case (A-Z, 0-9, and take away 1, 0, I, O).
This is my try
<?php
function generateRandomString($length = 5) {
return substr(str_shuffle("23456789ABCDEFGHIJKMNPQRSTUVWXYZ"), 0, $length);
}
echo generateRandomString();
?>
but i am not sure if there is a better way of doing this
If you need to call this function lots of times, your current implementation will be very slow, because it uses much more calls of random function than it is necessary (if $length < 32). Also if your set of allowed characters is smaller than number of characters in the result, your current implementation will return wrong result too. And also your implementation does not allow repeating of characters in the result, but in the specification it is not forbidden to repeat characters.
A little more accurate solution is to use array_rand():
function generateRandomString($length = 5) {
$allowed = str_split('23456789ABCDEFGHIJKMNPQRSTUVWXYZ'); // it is enough to do it once
$res = '';
foreach (array_rand($allowed, $length) as $k)
$res .= $allowed[$k];
return $res;
}
function generateRandom($length = 5) {
$possibleChars = '123456789ABCDEFGHJKMNPQRSTUVWXYZ';
$rndString = '';
for ($i = 0; $i < $length; $i++) {
$rndString .= $possibleChars[rand(0, strlen($possibleChars) - 1)];
}
return $rndString;
}
echo generateRandom();
Here you can define the characters which you want to have in your random string.
The problem with your function is that any char will be just used 1 time per call. Its not really random. And the lenght of the random string would also be limited to the amount of characters you have.
For example: AAAAA is not possible with your function, with mine it is.
If you need a string longer than your charset, that method will fail. Please can you try the code below;
<?php
function generateRandomString($length = 5) {
$chars = "23456789ABCDEFGHIJKMNPQRSTUVWXYZ"; //Your char-set
$charArray = str_split($chars); //Your array representation of chars
$charCount = strlen($chars); //Your char-set length
$result = "";
//Loop throught required `$length`
for($i=1;$i<=$length;$i++)
{
$randChar = rand(0,$charCount-1); //Pick a random char in range of our chars
$result .= $charArray[$randChar]; //Concatenate picked char to result
}
return $result;
}
echo generateRandomString(75);
?>
Here is a working example: https://ideone.com/D1EQ9T
Hope this helps.
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);
}