login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Handige scripts > str_rand()

str_rand()

Auteur: CelestialCelebi - 01 september 2004 - 19:25 - Gekeurd door: Dennisvb - Hits: 2984 - Aantal punten: 2.13 (4 stemmen)




Check uitleg? :x Hij maakt een willekeurige string van $iLength, met de parameters die jij opgeeft.

  1. <?php
  2. echo str_rand(32, true, true, true); // MD5..
  3. echo str_rand(400, false, false, false); // error
  4. echo str_rand(0, true, true, false); // error
  5. echo str_rand(4000000, false, false, true); // alleen nummers
  6. ?>

Code:
  1. <?php
  2. /*
  3. ***************************************************************************
  4. * Function str_rand(), used to generate a random string of the characters *
  5. * you specify, with a length of your choice. *
  6. * Copyright (C) 2004 The Celestial Celebi *
  7. * This program is free software; you can redistribute it and/or modify it *
  8. * under the terms of the GNU General Public License as published by the *
  9. * Free Software Foundation; either version 2 of the License, or (at your *
  10. * option) any later version. *
  11. * This program is distributed in the hope that it will be useful, but *
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
  14. * General Public License for more details. *
  15. * You should have received a copy of the GNU General Public License along *
  16. * with this program; if not, write to the Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************
  19. */
  20.  
  21. /*
  22. ***************************************************************************
  23. * @function: str_rand(): Returns a random string, created from the *
  24. * characters you specify. *
  25. * @param $iLength (int): The length of the random string. *
  26. * @param $bUseLowercase (bool): Use lowercase characters in the string? *
  27. * @param $bUseUppercase (bool): Use uppercase characters in the string? *
  28. * @param $bUseNumbers (bool): Use numbers in the string? *
  29. * @return string: The random string of $iLength characters. *
  30. *-------------------------------------------------------------------------*
  31. * @started on: 23 June 2004 at 20:00:00 by The Celestial Celebi. *
  32. * @last edited on: 23 June 2004 at 20:01:43 by The Celestial Celebi. *
  33. * @constructs used: array, if, for, return. *
  34. * @functions used: array_merge, count, rand, range. *
  35. ***************************************************************************
  36. */
  37.  
  38. function str_rand($iLength, $bUseLowercase, $bUseUppercase, $bUseNumbers)
  39. {
  40. if((!$bUseLowercase && !$bUseUppercase && !$bUseNumbers) || $iLength == 0)
  41. {
  42. trigger_error('Must use something to generate the string', E_USER_WARNING);
  43. }
  44. else
  45. {
  46. $aCharactersToChoose = array();
  47. if($bUseLowercase)
  48. {
  49. $aCharactersToChoose = array_merge($aCharactersToChoose, range('a', 'z'));
  50. }
  51. if($bUseUppercase)
  52. {
  53. $aCharactersToChoose = array_merge($aCharactersToChoose, range('A', 'Z'));
  54. }
  55. if($bUsenumbers)
  56. {
  57. $aCharactersToChoose = array_merge($aCharactersToChoose, range(0, 9));
  58. }
  59. $iNumCharacters = (count($aCharactersToChoose) - 1);
  60. $sRandomString = '';
  61. for($i = 0; $i <= $iLength; $i++)
  62. {
  63. $sRandomString .= $aCharactersToChoose[rand(0, $iNumCharacters)];
  64. }
  65. return $sRandomString;
  66. }
  67. }
  68. ?>
Download code! Download code (.txt)

 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Geen reacties (0)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.031s