My script read emails from file text.txt, i want to record emails and unique random code for every email in mySQL database. All works correct, but everytime i get the same "random code".
I get this:
example1#stack.com vsdggd
example2#stack.com vsdggd
example3#stack.com vsdggd
I want to get:
example1#stack.com bsgfdg
example2#stack.com jhngfv
example3#stack.com sdfasd
Code:
<?php
function generatePassword($length = 6){
$chars = 'abcdefhiknrstyz';
$numChars = strlen($chars);
$string = '';
for ($i = 0; $i < $length; $i++) {
$string .= substr($chars, rand(1, $numChars) - 1, 1);
}
return $string;
}
include('../connect.php');
$query = 'insert into cc (email,token) values ("%s","'. generatePassword(6) .'");';
$lines = file('text.txt');//your filename
for($i=0; $i < count($lines); $i++){
mysql_query(sprintf($query, mysql_real_escape_string($lines[$i]))) or die(mysql_error() . '<br><br>' . '<b>Query:</b> ' . $query);
}
?>
You are only calling generatePassword() once, to fix your code as it is, you want:
<?php
function generatePassword($length = 6){
$chars = 'abcdefhiknrstyz';
$numChars = strlen($chars);
$string = '';
for ($i = 0; $i < $length; $i++) {
$string .= substr($chars, rand(1, $numChars) - 1, 1);
}
return $string;
}
include('../connect.php');
$query = 'insert into cc (email,token) values ("%s","%s");';
$lines = file('text.txt');//your filename
for($i=0; $i < count($lines); $i++){
mysql_query(sprintf($query, mysql_real_escape_string($lines[$i]), generatePassword(6))) or die(mysql_error() . '<br><br>' . '<b>Query:</b> ' . $query);
}
?>
So that generatePassword() gets called for each row.
Also you should really be using the newer mysqli or ideally PDO as the older mysql is deprecated and has been removed from PHP7.
Related
I'm having the problem that i want to echo the class $password as often, as the number of $intcount is. Can you give me any idea how I would do that?
f.e. that I echo 4 random words if $intcount is 4, 5 words if its 5 etc etc.
<?php
$csv = array_map('str_getcsv', file('gen.csv'));
$count = $_GET['count'];
$intcount = (int)$count;
$min = 0;
$max = 4;
$rand = mt_rand( $min , $max );
$out = $csv[$rand][$rand];
$password = $out;
// var_dump($count);
// var_dump($intcount);
//
// echo $count . "\n <br>";
// echo $password * $intcount;
//
// for ($i=0; $i < $intcount ; $i++) {
// echo $password;
// }
?>
The commented code doesn't need to stay necessarily.
Thanks :)
You need to create the $rand inside the loop like:
$csv = array_map('str_getcsv', file('gen.csv'));
$count = $_GET['count'];
$intcount = (int)$count;
$min = 0;
$max = 4;
for ($i=0; $i < $intcount ; $i++)
{
$rand = mt_rand($min, $max);
$password = $csv[$rand][$rand];
echo $password;
}
I am using codes like "gjhyYhK", "HJjhkeuJ" etc. But want user to show these codes like:
gj_y__K
HJj__e_J
means code will be edited with "_" at random positions in code.
This will do what you want:
$str = "gjhyYhK";
$len = strlen($str);
$num_to_remove = ceil($len * .4); // 40% removal
for($i = 0; $i < $num_to_remove; $i++)
{
$k = 0;
do
{
$k = rand(1, $len);
} while($str[$k-1] == "_");
$str[$k-1] = "_";
}
print $str . "\n";
If you want more underscores, change the value of $underscores. This will guarantee you get how many underscores you want, so long as you want fewer than the length of the string
Try this:
$string=array(
'gjhyYhK',
'HJjhkeuJ'
);
$arr=array();
foreach ($string as $key=>$value) {
$arr[$key]='';
for ($i=1; $i <=strlen($value); $i++) {
if(rand(0,1)){
$arr[$key].=substr($string[$key],$i,1);
}else{
$arr[$key].='_';
}
}
}
var_dump($arr);
you can try below code to get the functionality what you are looking for
<?php
$string = "gjhyYhK";
$percentage = 40;
$total_length = strlen($string);
$number_of_underscore = floor(($percentage / 100) * $total_length); // I have use floor value, you can use ceil() as well
for ($i = 1; $i <= $number_of_underscore; $i++)
{
$random_position = rand(0, strlen($string) - 1); // get the random position of character to be replaced
if (substr($string, $random_position, 1) !== '_') // check if its already replaced underscore (_)
{
$string = preg_replace("/" . (substr($string, $random_position, 1)) . "/", '_', $string, 1); // here preg_replaced use to replace the character only once, (i.e str_replace() will replace all matching characters)
}
else
{
$i--; // else decrement $i for the loop to run one more time
}
}
echo $string;
?>
let me know if any other help needed
$str = "ADFJ";
$strlen = strlen($str);
$newStr = '';
for ($i = 0; $i < $strlen; $i++) {
if ($i == rand(0, $strlen)) {
$newStr .= '_';
} else {
$newStr .= $str[$i];
}
}
echo $newStr;
I'm trying to keep the for loop variable but I don´t know how,
This is the code that I use to make a string like: 1,2,3,4,5,6,7,8,9,10 ect.
for($i = 0; $i <= 17; $i++) {
$str = $i . ',';
}
Than:
$str = substr($str, 0, -1);
To get rid of the last char.
However, when I call the $str variable out of the for loop, it only outputs 17
Here is the whole code:
for($i = 0; $i <= 17; $i++)
{
$str = $i . ',';
}
$str = substr($str, 0, -1);
echo $str;
So to sum it up, I need the output to be 1,2,3,4,5,6,7,8,9,10 without a , at the end...
for($i = 0; $i <= 17; $i++)
{
// here
$str .= $i . ',';
}
$str = substr($str, 0, -1);
echo $str;
But there is a better way:
echo implode(',', range(0, 17));
I found this Stack Overflow post explaining how you can generate random coupon codes.
I'm looking into using that code and generate multiple coupons at once (e.g. 50), while separate them by a comma.
The output would be: COUPON-HMECN, COUPON-UYSNC, etc.
Code below and codepad example available.
$chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$res = "COUPON-";
for ($i = 0; $i < 5; $i++) {
$res .= $chars[mt_rand(0, strlen($chars)-1)];
}
echo $res . ",";
$chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$numCodesToGenerate = 5;
for ($n = 0; $n < $numCodesToGenerate; $n++)
{
$res = "COUPON-";
for ($i = 0; $i < 5; $i++) {
$res .= $chars[mt_rand(0, strlen($chars)-1)];
}
echo $res . ",";
}
Why not use uniqid()?
$coupon_str = '';
$seperator = '';
for($i = 0; $i < 50; $i++) {
$coupon_str .= $seperator . uniqid('COUPON-');
$seperator = ',';
}
echo $coupon_str;
Output:
COUPON-502373ac95dd2,COUPON-502373ac95de8,COUPON-502373ac95ded,....
Here is a much neater version (and faster) that does what you need:
function MakeCouponCode() {
$res = "COUPON-";
for($i = 0; $i < 5; ++$i)
$res .= chr(mt_rand(0, 1) == 0 ? mt_rand(65, 90) : mt_rand(48, 57));
return $res;
}
$coupons = array();
for($i = 0; $i < 5; ++$i)
$coupons[] = MakeCouponCode();
echo implode(', ', $coupons);
Output:
COUPON-D707Y, COUPON-4B37E, COUPON-3O397, COUPON-M799X, COUPON-24Q36
You can use the coupon code generator PHP class file to generate N number of coupons and its customizable, with various options of adding own mask with own prefix and suffix. The coupon codes are separated by comma. Simple PHP coupon code generator
Example:
coupon::generate(8); // J5BST6NQ
How can I iteratively create a variant of "Murrays" that has an apostrophe after each letter? The end-result should be:
"m'rrays,mu'rrays,mur'rays,murr'ays,murra'ys,murray's"
My suggestion:
<?php
function generate($str, $add, $separator = ',')
{
$split = str_split($str);
$total = count($split) - 1;
$new = '';
for ($i = 0; $i < $total; $i++)
{
$aux = $split;
$aux[$i+1] = "'" . $aux[$i+1];
$new .= implode('', $aux).$separator;
}
return $new;
}
echo generate('murrays', "'");
?>
You want to iterate through the name, and re-print it with apostrophe's? Try the following:
<?php
$string = "murrays";
$array = str_split($string);
$length = count($array);
$output = "";
for ($i = 0; $i < $length; $i++) {
for($j = 0; $j < $length; $j++) {
$output .= $array[$j];
if ($j == $i)
$output.= "'";
}
if ($i < ($length - 1))
$output .= ",";
}
print $output;
?>
Here’s another solution:
$str = 'murrays';
$variants = array();
$head = '';
$tail = $str;
for ($i=1, $n=strlen($str); $i<$n; $i++) {
$head .= $tail[0];
$tail = substr($tail, 1);
$variants[] = $head . "'" . $tail;
}
var_dump(implode(',', $variants));
well that's why functionnal programming is here
this code works on OCAML and F#, you can easily make it running on C#
let generate str =
let rec gen_aux s index =
match index with
| String.length s -> [s]
| _ -> let part1 = String.substr s 0 index in
let part2 = String.substr s index (String.length s) in
(part1 ^ "'" ^ part2)::gen_aux s (index + 1)
in gen_aux str 1;;
generate "murrays";;
this code returns the original word as the end of the list, you can workaround that :)
Here you go:
$array = array_fill(0, strlen($string) - 1, $string);
implode(',', array_map(create_function('$string, $pos', 'return substr_replace($string, "\'", $pos + 1, 0);'), $array, array_keys($array)));