Ads removed from the pastie. \o/
Running costs stayed. :-(
Donations welcome!

Bitcoin: 1QC38uPBSy7WSYz1whM5SYTHAdhqkN6o8V
Ethereum: 0x0855778a6181a69cc7d5d08c71c4f5846010eb67
Verge: D8fo1fszrNmUK4kfQW5owhYMv3osPvgn2g
Recent entries
2020-03-31 19:47:46
No title specified
2020-03-31 19:43:55
movie
2020-03-31 19:24:18
film complet en hd
2020-03-31 19:15:43
movie
2020-03-31 19:10:34
Multi Video Full Movie Se...
2020-03-31 19:06:00
No title specified
2020-03-31 18:59:56
No title specified
2020-03-31 18:36:07
movie
2020-03-31 17:55:21
https://medium.com/@jeann...
2020-03-31 17:54:38
chjyjhj
Permanent entries
2019-10-14 13:05:09
Shift-JIS to UTF-8 mappin...
2014-10-14 21:12:28
Boost property tree - Ite...
2013-10-18 08:24:21
Resize images in a folder...
2013-04-29 17:23:35
Find bad words in a text,...
2013-02-12 20:26:45
Merge two boost property ...
2012-12-18 16:21:40
OpenCV: Access/modify ima...
2012-11-19 10:48:40
Reading a boost::property...
2012-04-10 00:11:46
Convert hex to string or ...
2012-02-18 00:12:04
Screw up []-operator for ...
2012-02-16 03:02:24
Merge continuous numbers ...
2012-01-18 22:26:01
(PHP) Generate / Create (...
2011-04-19 16:05:25
Calculate one's age in ye...
2011-04-18 15:26:45
(PHP) Generate / Create r...
2010-08-26 15:18:00
Pick the first element (k...
2010-07-28 10:36:45
Quakenet - Snailbot comma...
2010-06-21 21:30:52
Using the MATLAB profiler...
2010-05-31 06:31:14
PHP truncation function
2010-05-06 20:49:26
Quakenet - Fishbot comman...
Paste ID: # whLB5
Date posted: Mon, 18 Apr 2011 15:26:45 +0000
Date of expiration: never (permanent entry)
Description:

(PHP) Generate / Create random string with chars and numbers (alphanumeric) of desired length

  1. <?php
  2. /* Sometimes it is required to create random strings, e.g. as unique keys, where using hash
  3.  * functions is not suitable. Remember, hash functions return a hexadecimal representation of a
  4.  * calculated checksum, i.e. a hash value only consists of [0-9a-f].
  5.  *
  6.  * To gain a wider range of characters used the following function might be more appropriate.
  7.  */
  8.  
  9.  
  10. /** $len : desired length of random string (should be smaller or equal to strlen( $abc ))
  11.  */
  12. function rand_str( $len )
  13. {
  14.   // The alphabet the random string consists of
  15.   $abc = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  16.  
  17.   // The default length the random key should have
  18.   $defaultLength = 3;
  19.  
  20.   // Ensure $len is a valid number
  21.   // Should be less than or equal to strlen( $abc ) but at least $defaultLength
  22.   $len = max( min( intval( $len ), strlen( $abc )), $defaultLength );
  23.  
  24.   // Return snippet of random string as random string
  25.   return substr( str_shuffle( $abc ), 0, $len );
  26. }
  27. // Example usage with a length of 25 chars
  28. echo 'Your random string is: '.rand_str( 25 ); // Example output: Your random string is: URwXWGTMDAebriKmEV4zdqJ2I
  29.  
  30.  
  31.  
  32. /**Note that $abc gets shuffled in the above version.
  33.  * If a character is required to appear more than once, $abc must be tweaked e.g. as follows
  34.  * $rep : how often a char may be repeated
  35.  */
  36. function rand_str( $len, $rep )
  37. {
  38.   // The alphabet the random string consists of
  39.   $abc = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  40.  
  41.   // The default length the random key should have
  42.   $defaultLength = 3;
  43.  
  44.   // The default count of repetitions of a char in the random string
  45.   $defaultRepetitions = 1;
  46.  
  47.   // Ensure $len is a valid number
  48.   // Should be less than or equal to strlen( $abc ) but at least $defaultLength
  49.   $len = max( min( intval( $len ), strlen( $abc )), $defaultLength );
  50.  
  51.   // Max. repetitions of a char
  52.   // Should be less than or equal to $len but at least $defaultRepetitions
  53.   $rep = max( min( intval( $rep ), $len ), $defaultRepetitions );
  54.  
  55.   // Expand $abc. Of course you may concatenate only parts of $abc here, too.
  56.   $abc = str_repeat( $abc, $rep );
  57.  
  58.   // Return snippet of random string as random string
  59.   return substr( str_shuffle( $abc ), 0, $len );
  60. }
  61. // Example usage with a length of 25 chars and each char may appear up to 4 times
  62. echo 'Your random string is: '.rand_str( 25, 4 ); // Example output: Your random string is: 0eMtcllvAAbPgOYO1CRcEBw0h
  63. ?>
You can paste a correction or amendment to the upper paste or a new paste down below.