|
 |
str_shuffle (PHP 4 >= 4.3.0, PHP 5) str_shuffle -- Переставляет символы в строке Описаниеstring str_shuffle ( string str )
str_shuffle() переставляет символы в строке.
Выбирается одна возможная перестановка из всех возможных.
Пример 1. Пример использования str_shuffle()
<?php
$str = 'abcdef';
$shuffled = str_shuffle($str);
echo $shuffled;
?>
|
|
См. также описание функций shuffle() и
rand().
str_shuffle
nmoyroud at teledetection dot fr
03-Nov-2005 04:38
Just another update to password builder function. With this version, you can choose the number of alphanumeric characters to add and the number of non-alphanumeric characters. You obtain a more secure password. You can add another characters to the non-alphanumeric list if you need.
If you want a password with 8 alphanumeric and 3 non-alphanumeric characters in a random order, just call :
random_passwd(8,3);
function generatePasswd($numAlpha=6,$numNonAlpha=2) {
$pwd = '';
$listAlpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$listNonAlpha = ',;:!?.$/*-+&@_+;./*&?$-!,';
$start = mt_rand(1, (strlen($listAlpha)-$numAlpha));
$string = str_shuffle($listAlpha);
$pwd .= substr($string,$start,$numAlpha);
$start = mt_rand(1, (strlen($listNonAlpha)-$numNonAlpha));
$string = str_shuffle($listNonAlpha);
$pwd .= substr($string,$start,$numNonAlpha);
return str_shuffle($pwd);
}
support at wiredlabs dot net
15-Aug-2005 02:03
Just an update to sfalcon's function. Chooses a random number within the length and then selects a random segment not the current segment of 1 to $numchar. Also added upper-case chars.
function random_passwd($numchar=8) {
$str = "abcefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$start = mt_rand(1, (strlen($str)-$numchar));
$string = str_shuffle($str);
$password = substr($string,$start,$numchar);
return($password);
}
Qube, Efnet #php
05-Aug-2005 04:20
(correction)
I balked at the loop method used below in inverting and randomising capitalisation and released that we just need to flip the bit (3rd bit or 6th depending on what way you look at it) and it would cause the case change in a character.
Full code and example:
http://www.pgregg.com/projects/php/code/str_case.php
(append an s to the url to get the .phps source)
I came up with a method and thanks to arpad (optimising as ever! :) we now have:
Invert case:
preg_replace('/[a-z]/ie', '\'$0\' ^ str_pad(\'\', strlen(\'$0\'), \' \')', $input);
Randomise case:
preg_replace('/[a-z]/ie', '(rand(0,1) ? \'$0\' ^ str_pad(\'\', strlen(\'$0\'), \' \') : \'$0\')', $input);
and for good measure:
Upper case:
preg_replace('/[a-z]/e', '\'$0\' & str_pad(\'\', strlen(\'$0\'), chr(223))', $input);
Lower case:
preg_replace('/[A-Z]/e', '\'$0\' | str_pad(\'\', strlen(\'$0\'), \' \')', $input);
rudigreen at gmail dot com
01-Jul-2005 07:20
Here are 2 functions, one inverts the case of a string and the other one randomizes the captilization in a string, kinda
useful string operations.
<?
function invert_case($str)
{
$nstr = '';
for($i=0;$i<strlen($str);$i++)
{
$char = substr($str,$i,1);
if($char == strtolower($char))
{
$char = strtoupper($char);
}
else {
$char = strtolower($char);
}
$nstr .= $char;
}
return $nstr;
}
function rand_case($str)
{
$nstr = '';
for($i=0;$i<strlen($str);$i++)
{
$char = substr($str,$i,1);
$char = invert_case($char);
rand(0,1) == 1 ? $char = invert_case($char) : null;
$nstr .= $char;
}
return $nstr;
}
echo rand_case("pHp rooLz! gUyZ"); echo invert_case("pHP STRiNG sEx");?>
Michiel van den boogaard
17-Jan-2005 02:03
Shortend function for PHP < 4.3
<?php
function RandomPass($numchar)
{
$word = "a,b,c,d,e,f,g,h,i,j,k,l,m,1,2,3,4,5,6,7,8,9,0";
$array=explode(",",$word);
shuffle($array);
$newstring = implode($array,"");
return substr($newstring, 0, $numchar);
}
?>
sfalcon_(at)_pymesweb_(dot)_com
26-Aug-2004 08:28
You can use str_shuffle to make simple and nice random passwords:
function random_passwd($numchar) {
$string =str_shuffle("abcefghijklmnopqrstuvwxyz1234567890");
$password = substr($string,1,$numchar);
return($password);
}
echo random_passwd('8') // Will return something like "xa3dh214" =)
| |