I made a random code with md5, but how to make the code contains only numbers
$this->db->select('RIGHT(service.tracking_number,4) as kode', FALSE);
$this->db->order_by('id_service','DESC');
$this->db->limit(1);
$query = $this->db->get('service');
if($query->num_rows() <> 0){
$data = $query->row();
$kode = intval($data->kode) + 1;
}else {
$kode = 1;
}
$kodemax = str_pad($kode, 1, "0", STR_PAD_LEFT);
$kodejadi = "RJC".md5($kodemax);
$hasil = substr($kodejadi, 0,-15);
return $hasil;
output RJC1679091c5a880faf6
how to make these output numbers?
To generate a random number you can use this snippet:
$count = 10;
$key = '';
$chars = '0123456789';
for($x = 0; $x < $count; $x++) {
$key = $key.$chars[rand(0, strlen($chars) - 1)];
}
echo $key;
Related
How do I make sure that the password contains at least one character from each array? I could use do..while, right? What I don't quite understand is what I will need to put in the if statement inside do...while.
Is do...while the only way to ensure at least one character from each array is in the final password or are there other ways?
$length = 6;
$a1 = str_split('0123456789');
$a2 = str_split('%^*+~?!');
$a3 = str_split('abcdefghigklmnopqrstuvwxyz');
$result = ""; //final random password
for($i = 0; $i < $length; $i++){
$values = [$a1,$a2,$a3];
$chosen = array_rand($values);
$result .= $values[$chosen][array_rand($values[$chosen])];
}
echo $result;
$length = 6;
$a1 = str_split('0123456789');
$a2 = str_split('%^*+~?!');
$a3 = str_split('abcdefghigklmnopqrstuvwxyz');
$resultArr[] = $a1[array_rand($a1)];
$resultArr[] = $a2[array_rand($a2)];
$resultArr[] = $a3[array_rand($a3)];
for($i = 0; $i < $length-3; $i++){
$values = [$a1,$a2,$a3];
$chosen = array_rand($values);
$resultArr[] = $values[$chosen][array_rand($values[$chosen])];
}
shuffle($resultArr);
$result = implode($resultArr); //final random password
echo $result;
You can surround the for wit ha do..while and in the condition check with preg_match() and a regex pattern that checks if at least one character is in the result:
<?php
$length = 6;
$a1 = str_split('0123456789');
$a2 = str_split('%^*+~?!');
$a3 = str_split('abcdefghigklmnopqrstuvwxyz');
$result = ""; //final random password
do {
for($i = 0; $i < $length; $i++){
$values = [$a1,$a2,$a3];
$chosen = array_rand($values);
$result .= $values[$chosen][array_rand($values[$chosen])];
}
} while(!(preg_match('/^(?=.*?[0-9])(?=.*?[a-z])(?=.*?[%*+?!]).{1,}/', $result)));
echo $result;
If the there's no coincidences with the pattern using preg_match, then will return false, in the do..while condition we check if one preg_match is false, if it is, do it again.
I made my answer general for any number of arrays.
I would use preg_match() for this, but for the question you asked, yes, you need to check each array separately:
$user_inputted_password = $_POST['user_inputted_password'];
$length = strlen($user_inputted_password); // user input password - you are capturing this somewhere, right
$a1 = str_split('0123456789');
$a2 = str_split('%^*+~?!');
$a3 = str_split('abcdefghigklmnopqrstuvwxyz');
$validate = array();
$result = ""; //final random password
function populate_validation_array($match_num, &$validate) {
if (!in_array($match_num, $validate)) {
$validate[] = $match_num;
}
}
$numeric_indexes = array(1, 2, 3);
$index_count = count($numeric_indexes);
for($i = 0; $i < $length; $i++){
for($j = 0; $j < $index_count; $j++) {
$numeric_index = $numeric_indexes[$j];
if (in_array($user_inputted_password[$i], ${"a" . $numeric_index})) {
populate_validation_array($numeric_index, $validate);
}
}
}
if (count($validate) === $index_count) {
$values = array();
for($i = 0; $i < $index_count; $i++) {
$values[] = ${"a" . ($i + 1)};
}
$chosen = array_rand($values);
$result .= $values[$chosen][array_rand($values[$chosen])];
}
echo $result;
I implemented the functionality get the alphanumberic value and plus one value.
$string = "VDLE009567";
$person = $numbers = array();
if(preg_match_all('/([a-z ]+[0-9]+)/i', $string, $mt)) {
$nrmt = count($mt[0]);
for($i=0; $i<$nrmt; $i++) {
if(preg_match('/([a-z ]+)([0-9]+)/i', $mt[0][$i], $mt2)) {
echo $person[$i] = trim($mt2[1]);
echo $numbers[$i] = $mt2[2];
}
}
}
echo $numbers=$numbers[0]+1;
i need to get the values as "VDLE009568" but i got the values as "VDLE9568". Is there is any possible way to get this "VDLE009568"
for your required condition i made some changes in the code
<?php
$string = "VDLE999999";
$person = $numbers = array();
if(preg_match_all('/([a-z ]+[0-9]+)/i', $string, $mt)) {
$nrmt = count($mt[0]);
for($i=0; $i<$nrmt; $i++) {
if(preg_match('/([a-z ]+)([0-9]+)/i', $mt[0][$i], $mt2)) {
$person[$i] = trim($mt2[1]);
$numbers[$i] = $mt2[2];
}
}
}
$num = (string)$numbers[0];
$word_length = strlen($num);
$max = 9;
$max_str = '';
for($i=0;$i<$word_length;$i++){
$max_str .=$max;
}
$max_str = (int)$max_str;
if($num < $max_str){
echo $numbers=str_pad($num+1, $word_length, '0', STR_PAD_LEFT);
}else{
$numbers=str_pad($num+1,$word_length+1, '0', STR_PAD_LEFT);
$new_str = strrev((string)$numbers);
$new_num = (int)$new_str;
echo $numbers = str_pad($new_num,$word_length+1, '0', STR_PAD_LEFT);
}
Use this code for result
<?php
$string = "VDLE999999";
$person = $numbers = array();
if (preg_match_all('/([a-z ]+[0-9]+)/i', $string, $mt)) {
$nrmt = count($mt[0]);
for ($i = 0; $i < $nrmt; $i++) {
if (preg_match('/([a-z ]+)([0-9]+)/i', $mt[0][$i], $mt2)) {
$personout = $person[$i] = trim($mt2[1]);
$numbers[$i] = $mt2[2];
if ($numbers[0] == 999999) {
$last = substr($personout, -1, 1);
$personout = substr($personout, 0, -1) . (++$last);
$numbers[0] = 0;
}
echo $personout;
}
}
}
echo $numbers = str_pad($numbers[0] + 1, 6, '0', STR_PAD_LEFT);
For result : VDLF000001 Online Test Click Here
Here, there is a example string "XjYAKpR" .. how to create all new string possibility with that string ??
I've tried before
function containAllRots($s, $arr) {
$n = strlen($s);
$a = array();
for ($i = 0; $i < $n ; $i++) {
$rotated = rotate(str_split($s), $i);
$a[] = $rotated;
}
print_r($a);die();
if (array_diff($arr, $a)) {
return True;
}
else
{
return False;
}
}
I make 2 function rotate and generate
function rotate($l, $n) {
$b = $l[$n];
$sisa = array_values(array_diff($l, array($b)));
for ($i = 0; $i < count($sisa) ; $i++) {
$random[] = generate($sisa, $b);
}
print_r($random);die();
$hasil = $l[$n] . implode("",$random);
return $hasil;
}
function generate($sisa, $b) {
$string = implode("",$sisa);
$length = count($sisa);
$size = strlen($string);
$str = '';
for( $i = 0; $i < $length; $i++ ) {
$str .= $string[ rand( 0, $size - 1 ) ];
}
Here there is a pair of functions that lets you calculate a permutation set
(no repetitions are taken in account)
function extends_permutation($char, $perm) {
$result = [];
$times = count($perm);
for ($i=0; $i<$times; $i++) {
$temp = $perm;
array_splice($temp, $i, 0, $char);
array_push($result, $temp);
}
array_push($result, array_merge($perm, [$char]));
return $result;
}
function extends_set_of_permutations($char, $set) {
$step = [];
foreach ($set as $perm) {
$step = array_merge($step, extends_permutation($char, $perm));
}
return $step;
}
you can use them to generate the required set of permutations. Something like this:
$seed = "XjYAKpR";
// the first set of permutations contains only the
// possible permutation of a one char string (1)
$result_set = [[$seed[0]]];
$rest = str_split(substr($seed,1));
foreach($rest as $char) {
$result_set = extends_set_of_permutations($char, $result_set);
}
$result_set = array_map('implode', $result_set);
sort($result_set);
At the end of the execution you will have the 5040 permutations generated by your string in the result_set array (sorted in alphabetical order).
Add a char and you will have more than 40000 results.
The functions are quite naive in implementation and naming, both aspects can be improved.
already look around but cant find what i want for PHP.
just say i have a number : 1234 ( can be splitted first into array )
and i want to get how many number combination possible for 2 digits, 3 digits , and 4 digits
for example :
possible 4 digits will be :
1234,1243,1324,1342, and so on. ( i dont know how many more )
possible 2 digits will be :
12,13,14,21,23,24,31,32,34,41,42,43
the closest one i get is :
$p = permutate(array('1','2','3','4'));
$result = array();
foreach($p as $perm) {
$result[]=join("",$perm);
}
$result = array_unique($result);
print join("|", $result);
function permutate($elements, $perm = array(), &$permArray = array()){
if(empty($elements)){
array_push($permArray,$perm); return;
}
for($i=0;$i<=count($elements)-1;$i++){
array_push($perm,$elements[$i]);
$tmp = $elements; array_splice($tmp,$i,1);
permutate($tmp,$perm,$permArray);
array_pop($perm);
}
return $permArray;
}
but how can i edit this so i can display for 3 and 2 digits ?
Thanks
i got what i want
it's from #mudasobwa link. and i edit to what i want.
<?php
$in = array(1,2,3,4,5,6);
$te = power_perms($in);
// print_r($te);
$thou=0;
$hun =0;
$pu = 0;
for($i=0;$i<count($te);$i++)
{
$jm = count($te[$i]);
for($j=0;$j<$jm;$j++)
{
$hsl[$i] = $hsl[$i] . $te[$i][$j];
}
if($hsl[$i] >=100 && $hsl[$i] < 1000 )
{
$ratus[$hun] = intval($hsl[$i]);
$hun = $hun + 1;
}
if($hsl[$i] <100 && $hsl[$i] >=10)
{
$pul[$pu] = intval($hsl[$i]);
$pu = $pu + 1;
}
if($hsl[$i] >=1000 && $hsl[$i] < 10000)
{
$th[$thou] = intval($hsl[$i]);
$thou = $thou + 1;
}
}
$th=array_unique($th);
$pul = array_unique($pul);
$ratus = array_unique($ratus);
sort($ratus);
sort($pul);
sort($th);
print_r($th);
function power_perms($arr) {
$power_set = power_set($arr);
$result = array();
foreach($power_set as $set) {
$perms = perms($set);
$result = array_merge($result,$perms);
}
return $result;
}
function power_set($in,$minLength = 1) {
$count = count($in);
$members = pow(2,$count);
$return = array();
for ($i = 0; $i < $members; $i++) {
$b = sprintf("%0".$count."b",$i);
$out = array();
for ($j = 0; $j < $count; $j++) {
if ($b{$j} == '1') $out[] = $in[$j];
}
if (count($out) >= $minLength) {
$return[] = $out;
}
}
// usort($return,"cmp"); //can sort here by length
return $return;
}
function factorial($int){
if($int < 2) {
return 1;
}
for($f = 2; $int-1 > 1; $f *= $int--);
return $f;
}
function perm($arr, $nth = null) {
if ($nth === null) {
return perms($arr);
}
$result = array();
$length = count($arr);
while ($length--) {
$f = factorial($length);
$p = floor($nth / $f);
$result[] = $arr[$p];
array_delete_by_key($arr, $p);
$nth -= $p * $f;
}
$result = array_merge($result,$arr);
return $result;
}
function perms($arr) {
$p = array();
for ($i=0; $i < factorial(count($arr)); $i++) {
$p[] = perm($arr, $i);
}
return $p;
}
function array_delete_by_key(&$array, $delete_key, $use_old_keys = FALSE) {
unset($array[$delete_key]);
if(!$use_old_keys) {
$array = array_values($array);
}
return TRUE;
}
?>
How can I select a random set of rows
The important bits:
I need to specify the number of random rows to select via a variable.
Say for instance the number of rows I want to select is 10, then it HAS TO select 10 DIFFERENT rows. I don't want it to pick out the same row a few times until it has 10.
The code below picks out 1 random row, how can I tailor this to the above spec?
<?php $rows = get_field('repeater_field_name');
$row_count = count($rows);
$i = rand(0, $row_count - 1);
echo $rows[$i]['sub_field_name']; ?>
<?php
$rows = get_field('repeater_field_name');
$row_count = count($rows);
$rand_rows = array();
for ($i = 0; $i < min($row_count, 10); $i++) {
// Find an index we haven't used already (FYI - this will not scale
// well for large $row_count...)
$r = rand(0, $row_count - 1);
while (array_search($r, $rand_rows) !== false) {
$r = rand(0, $row_count - 1);
}
$rand_rows[] = $r;
echo $rows[$r]['sub_field_name'];
}
?>
This is a better implementation:
<?
$rows_i_want = 10;
$rows = get_field('repeater_field_name');
// Pull out 10 random rows
$rand = array_rand($rows, min(count($rows), $rows_i_want));
// Shuffle the array
shuffle($rand);
foreach ($rand as $row) {
echo $rows[$row]['sub_field_name'];
}
?>
Simply loop through the random row process the number of random rows you want to get.
<?php
$rows_to_get=10;
$rows = get_field('repeater_field_name');
$row_count = count($rows);
$x=0
while($x<$rows_to_get){
echo $rows[rand(0, $row_count - 1)]['sub_field_name'];
$x++;
}
?>
You can give this a try
$rows = get_field('repeater_field_name');
var_dump(__myRand($rows, 10));
function __myRand($rows, $total = 1) {
$rowCount = count($rows);
$output = array();
$x = 0;
$i = mt_rand(0, $rowCount - 1);
while ( $x < $total ) {
if (array_key_exists($i, $output)) {
$i = mt_rand(0, $rowCount - 1);
} else {
$output[$i] = $rows[$i]['sub_field_name'];
$x ++;
}
}
return $output ;
}
A simple solution :
$rows = get_field('repeater_field_name');
$limit = 10;
// build new array
$data = array();
foreach ($rows as $r) { $data[] = $r['sub_field_name']; }
shuffle($data);
$data = array_slice($data, 0, min(count($data), $limit));
foreach ($data as $val) {
// do what you want
echo $val;
}