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.
<?php
echo str_rand(32, true, true, true); // MD5..
echo str_rand(400, false, false, false); // error
echo str_rand(0, true, true, false); // error
echo str_rand(4000000, false, false, true); // alleen nummers
?>
<?php echo str_rand (32, true, true, true); // MD5.. echo str_rand (400, false, false, false); // error echo str_rand (0, true, true, false); // error echo str_rand (4000000, false, false, true); // alleen nummers ?>
|
Code: |
<?php
/*
***************************************************************************
* Function str_rand(), used to generate a random string of the characters *
* you specify, with a length of your choice. *
* Copyright (C) 2004 The Celestial Celebi *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation; either version 2 of the License, or (at your *
* option) any later version. *
* This program is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************
*/
/*
***************************************************************************
* @function: str_rand(): Returns a random string, created from the *
* characters you specify. *
* @param $iLength (int): The length of the random string. *
* @param $bUseLowercase (bool): Use lowercase characters in the string? *
* @param $bUseUppercase (bool): Use uppercase characters in the string? *
* @param $bUseNumbers (bool): Use numbers in the string? *
* @return string: The random string of $iLength characters. *
*-------------------------------------------------------------------------*
* @started on: 23 June 2004 at 20:00:00 by The Celestial Celebi. *
* @last edited on: 23 June 2004 at 20:01:43 by The Celestial Celebi. *
* @constructs used: array, if, for, return. *
* @functions used: array_merge, count, rand, range. *
***************************************************************************
*/
function str_rand($iLength, $bUseLowercase, $bUseUppercase, $bUseNumbers)
{
if((!$bUseLowercase && !$bUseUppercase && !$bUseNumbers) || $iLength == 0)
{
trigger_error('Must use something to generate the string', E_USER_WARNING);
}
else
{
$aCharactersToChoose = array();
if($bUseLowercase)
{
$aCharactersToChoose = array_merge($aCharactersToChoose, range('a', 'z'));
}
if($bUseUppercase)
{
$aCharactersToChoose = array_merge($aCharactersToChoose, range('A', 'Z'));
}
if($bUsenumbers)
{
$aCharactersToChoose = array_merge($aCharactersToChoose, range(0, 9));
}
$iNumCharacters = (count($aCharactersToChoose) - 1);
$sRandomString = '';
for($i = 0; $i <= $iLength; $i++)
{
$sRandomString .= $aCharactersToChoose[rand(0, $iNumCharacters)];
}
return $sRandomString;
}
}
?>
<?php /* *************************************************************************** * Function str_rand(), used to generate a random string of the characters * * you specify, with a length of your choice. * * Copyright (C) 2004 The Celestial Celebi * * This program is free software; you can redistribute it and/or modify it * * under the terms of the GNU General Public License as published by the * * Free Software Foundation; either version 2 of the License, or (at your * * option) any later version. * * This program is distributed in the hope that it will be useful, but * * WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * * General Public License for more details. * * You should have received a copy of the GNU General Public License along * * with this program; if not, write to the Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * *************************************************************************** */ /* *************************************************************************** * @function: str_rand(): Returns a random string, created from the * * characters you specify. * * @param $iLength (int): The length of the random string. * * @param $bUseLowercase (bool): Use lowercase characters in the string? * * @param $bUseUppercase (bool): Use uppercase characters in the string? * * @param $bUseNumbers (bool): Use numbers in the string? * * @return string: The random string of $iLength characters. * *-------------------------------------------------------------------------* * @started on: 23 June 2004 at 20:00:00 by The Celestial Celebi. * * @last edited on: 23 June 2004 at 20:01:43 by The Celestial Celebi. * * @constructs used: array, if, for, return. * * @functions used: array_merge, count, rand, range. * *************************************************************************** */ function str_rand($iLength, $bUseLowercase, $bUseUppercase, $bUseNumbers) { if((!$bUseLowercase && !$bUseUppercase && !$bUseNumbers) || $iLength == 0) { trigger_error('Must use something to generate the string', E_USER_WARNING); } else { $aCharactersToChoose = array(); if($bUseLowercase) { } if($bUseUppercase) { } if($bUsenumbers) { } $iNumCharacters = (count($aCharactersToChoose) - 1); $sRandomString = ''; for($i = 0; $i <= $iLength; $i++) { $sRandomString .= $aCharactersToChoose[rand(0, $iNumCharacters)]; } return $sRandomString; } } ?>
Download code (.txt)
|
|
Stemmen |
Niet ingelogd. |
|