|
Random string functie
Het maakt een random string aan, met de gegevens die jij opgeeft. Let op dat je niet voor alle drie de parameters false opgeeft, want dan krijg je een foutmelding.
Meer uitleg: Zie script. Een heel blok. :D
<?php
/*
***************************************************************************
* Function random_string(), 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: random_string(): 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 random_string($iLength, $bUseLowercase, $bUseUppercase, $bUseNumbers)
{
$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;
}
?>
download het script
Reacties van leden
Auteur: DaddyDJ @ 08-10-2004
Kan ook anders:
<?php
function random_string()
{
$aArguments = func_get_args();
if(count($aArguments) == 1)
{
return func_get_arg(0);
}
elseif(empty($aArguments))
{
trigger_error("Wrong parameter count for random_string()..",E_USER_WARNING);
}
else
{
return $aArguments[rand(0,count($aArguments)];
}
}
echo random_string("a","b","c");
?>
|