0

I don't really know how to go about but it really pretty for me in achievement like each rand_string to each index.

My code:

function rand_string($length) {
  $str="";
  $chars = "abcdefghijklmanopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  $size = strlen($chars);
  for($i = 0; $i < $length; $i++) {
    $str .= $chars[rand(0, $size-1)];
  }
  return $str;
}

$pcode = rand_string(4);
for ($b = 0; $b < 3; $b++) {
  echo $pcode[$b];
}

I am expecting something like: 9cwm cZnu c9e4 in the output. Can I achieve this in PHP?

Currently, with my code, I get a string from rand_string in each index like 9cw.

t j
  • 7,026
  • 12
  • 46
  • 66
  • Could you just redact your post(title included) so we could get your intentions more clear? Right now I could only guess that you wanna print 3 random strings... so you should instead of what you are doing now just echo rand_string(4) inside of that second loop. – Arek Bal Nov 23 '17 at 23:26
  • You have `a` in `$chars` twice. – Sammitch Nov 24 '17 at 00:02
  • Possible duplicate of [PHP: How to generate a random, unique, alphanumeric string?](https://stackoverflow.com/questions/1846202/php-how-to-generate-a-random-unique-alphanumeric-string) – mickmackusa Nov 24 '17 at 01:32
  • Updated the formatting and cleaned up some of the grammar. – t j Nov 24 '17 at 03:16

3 Answers3

1

Your code works, you only need to call rand_string inside your second loop in order to get something like 9cwm cZnu c9e4 (what you have described in your question).

Here is a working example:

function rand_string($length) {
    $str="";
    $chars = "abcdefghijklmanopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $size = strlen($chars);
    for($i = 0;$i < $length;$i++) {
        $str .= $chars[rand(0,$size-1)];
    }
    return $str;
}

// call rand_string inside for loop
for ($b = 0; $b<3; $b++) {
    echo rand_string(4).' ';
}

Try it online

YouneL
  • 8,152
  • 2
  • 28
  • 50
  • in more tips can i get back these string in one line? Example: `string(12) "9cwmcZnuc9e4"` – Kingsley Plaza Nov 24 '17 at 01:00
  • Yes Of course, just do `echo rand_string(4);` without concatinating ' ' in the last loop – YouneL Nov 24 '17 at 01:02
  • Geting this string back in one line is in hard size dont seem to be working as expect do a checkup – Kingsley Plaza Nov 24 '17 at 10:12
  • Do you want to be like this: [Demo](http://sandbox.onlinephpfunctions.com/code/5f6430e7bee33ac7af27bd7add08c245fa629b80) ? – YouneL Nov 24 '17 at 10:30
  • Yes but in one var_dump string line example: function rand_string($length) { $str=""; $chars = "abcdefghijklmanopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $size = strlen($chars); for($i = 0;$i < $length;$i++) { $str .= $chars[rand(0,$size-1)]; } return $str; } // call rand_string inside for loop for ($b = 0; $b<3; $b++) { $onestringline = rand_string(4); var_dump($onestringline); } expecting string( 12) "jd9mjjmdagja" – Kingsley Plaza Nov 24 '17 at 10:51
  • You have to use a variable to collect random string and then use var_dump to show it: [Example](http://sandbox.onlinephpfunctions.com/code/4014418920358f72ba802ebfbdb82bdeff05e0e6) – YouneL Nov 24 '17 at 10:58
  • Yes very wish never thought big thanks for long time coparation – Kingsley Plaza Nov 24 '17 at 11:16
0

The generation of the string actually works in your code. You aren't calling/printing the function correctly. Just call the function three times and print all the results (with spaces in between). Instead of printing you could join it together in a string and remove the last space.

<?php
function rand_string($length) {
  $str="";
  $chars = "abcdefghijklmanopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  $size = strlen($chars);
  for($i = 0;$i < $length;$i++) {
   $str .= $chars[rand(0,$size-1)];
  }
  return $str;
 }

  for ($b = 0; $b<3; $b++)
   {
    echo rand_string(4)." ";
    }
Niellles
  • 868
  • 10
  • 27
-1

If you don't mind using a completely different approach, limited to 32 chars :

return substr(md5(mt_rand().time()), 0, $length);

It's not super random but you get the picture...

Thanks to CM and user2693053 for bringing stuff to my attention (updated answer)

  • using mt_rand() instead of rand()
  • md5() length of 32...
hexYeah
  • 1,040
  • 2
  • 14
  • 24
  • i can't detect your method please edit with more codes or idea thanks – Kingsley Plaza Nov 23 '17 at 23:32
  • The idea is that you use `md5(rand().time()` to generate a reasonably random string by hashing a combination of a random number and the current time. After that `substr()` is used to return a part of that string with length `$length`. Keep in mind that `md5()` returns a string of 32 characters, so work around that if you can have `$length > 32`. – Niellles Nov 23 '17 at 23:38
  • `rand()` is not recommended and is an alias for `mt_rand()` in PHP 7. For backwards compatibility reasons you should use `mt_rand()` instead. Hope I helped! -CM – Cole Nov 23 '17 at 23:42
  • Thank you guys for your comments ! I will update my answer and credit you ;) – hexYeah Nov 23 '17 at 23:56