Based on this request >>>
http://www.xyplorer.com/xyfc/viewtopic. ... 491#p57491
Thanks to Don for always improving scripting functionality.
Code: Select all
//XYplorer script to randomize an string
//found at http://www.xyplorer.com/xyfc/viewtopic.php?f=5&t=4780&p=57721
$b="";//dummy for nicer layout
// === === === === user settings === === === ===
$IN = "This Should be RaNdoM"; //the input string to process on
//d,e,R,s,h,i,T,M,u,S,N,o,l,o,d,b,a,h //$out would be like this
$RandomizeCaseToo = false; //change the case of current char $sign?
$delim = ","; //delim the single chars by $delim
$DropSpaces = TRUE; //remove spaces from output?
// === === === === === === === === === === === ===
// === === === Don't modify code below === === ===
//user-settings: wants spaces or not?
if ($DropSpaces){ $str = replace($IN, " ", ""); }else {$str = $IN;}
$count=0; $steps=strlen($str); $out=""; //inits
while($count < $steps) //for each char in str
{
$len = strlen($str);
if ($len > 1) //if there is more then one sign in str
{
$rand = rand(0,$len); //get an random number to get random sign from str
if ($rand == 0) //if random sign the first sign from str
{
$sign = substr($str, 0, 1); //--get the random sign into an var
$str = substr($str, 1); //--set str to str without this random sign
}elseif ($rand == $len) //if random sign the last sign from str
{
$sign = substr($str, $len -1, 1); //--get sign
$str = substr($str, 0, $len -1); //--remove sign from str
}else //if random sign is somewhere inside str
{
$sign = substr($str, $rand, 1); //--get sign
$subA = substr($str, 0, $rand); //--get part of str before sign
$subB = substr($str, $rand +1); //--get part of str after sign
$str = $subA$subB; //--set str to str without sign
}
}else
{
$sign = $str; //if only one sign left in str simply use this
}
if ($RandomizeCaseToo) //User-settings: randomize case or not?
{
$case = rand(0,1); //random: do an case change or not?
if ($case==1){ $sign = recase($sign, "upper");}
}
$out = "$out$sign$delim"; //collect output: OutArray+Sign+Delim
$count++; //increase count
}
//trim surrounding spaces and comas:
$out = regexreplace($out, "[ ,]*(\b.+\b)[ ,]*", "$1");
// === create output ===
//just use $out somewhere.
// would be N,a,T,s,h,d,i,S,M,o,b,R,e,l,u,h,o,d
// or oodaLiBR u D MSThSNeh
// depending on user-settings.
//-----------------------------------------------------------------
//--------create an test output:
$lenIn = strlen($IN);
$lenOut = strlen($out);
text RandomizeCaseToo is: $RandomizeCaseToo<crlf>
Delimiter is: "$delim"<crlf>
DropSpaces is: $DropSpaces<crlf 2>
Original string ($lenIn):<crlf>$IN<crlf 2>
Output is ($lenOut):<crlf>$out;
/*
Our test Output would look like:
RandomizeCaseToo is: 0
Delimiter is: ","
DropSpaces is: 1
Original string (21):
This Should be RaNdoM
Output is (35):
N,a,T,s,h,d,i,S,M,o,b,R,e,l,u,h,o,d
-------------
Or:
RandomizeCaseToo is: 1
Delimiter is: ""
DropSpaces is: 0
Original string (21):
This Should be RaNdoM
Output is (21):
oodaLiBR u D MSThSNeh
*/
########### EDIT:
Code: Select all
v9.90.0505 - 2011-03-20
* SC replace enhanced. Added optional parameters start and count.
Syntax: replace(string, search, replacement, [matchcase], _
[start=1], [count=-1]))
start: Position from left where the replacement starts.
count: Maximal number of replacements.
* SC recase enhanced. New named argument for parameter mode:
mode:
invert: Invert the case of each letter.
Example:
text recase("the caMel can't.", "invert"); //THE CAmEL CAN'T.
The above rewrote Script:
Code: Select all
//XYplorer script to randomize an string
//found at http://www.xyplorer.com/xyfc/viewtopic.php?f=5&t=4780&p=57721
$b="";//dummy for nicer layout
global $deb; //set global var for pass info to the debugging function in sub routine
// === === === === user settings === === === ===
$IN = "This Should be RaNdoM"; //the input string to process on
//d,e,R,s,h,i,T,M,u,S,N,o,l,o,d,b,a,h //$out would be like this
$RandomizeCaseToo = TRUE; //change the case of current char $sign?
$delim = ","; //delim the single chars by $delim
$DropSpaces = TRUE; //remove spaces from output?
// === === === === === === === === === === === ===
// === === === Don't modify code below === === ===
//user-settings: wants spaces or not?
if ($DropSpaces){ $str = replace($IN, " ", ""); }else {$str = $IN;}
$count=0; $steps=strlen($str); $out=""; //inits
while($count < $steps) //for each char in str
{
$len = strlen($str);
if ($len > 1) //if there is more then one sign in str
{
$rand = rand(0,$len); //get an random number to get random sign from str
$deb = "RAND $rand, str: $str, len: $len"; //set text for debugging output
Sub "_DEBUG"; //call debug sub routine with $deb as parameter
if ($rand == 0) //if random sign the first sign from str
{
$sign = substr($str, 0, 1); //--get the random sign into an var
$str = replace($str, $sign, "", 1, 1, 1); //--set str to str without this random sign
$deb = "RAND case 0 , SIGN: $sign , new STR: $str<crlf>"; Sub "_DEBUG"; //Debugging
}elseif ($rand == $len) //if random sign the last sign from str
{
$sign = substr($str, $len -1, 1); //--get sign
$str = replace($str, $sign, "", 1, 1, 1); //--remove sign from str
$deb = "RAND case len , SIGN: $sign , new STR: $str<crlf>"; Sub "_DEBUG";
}else //if random sign is somewhere inside str
{
$sign = substr($str, $rand, 1); //--get sign
$str = replace($str, $sign, "", 1, 1, 1); //--remove sign from str
$deb = "RAND case else, SIGN: $sign , new STR: $str<crlf>"; Sub "_DEBUG"; //Debugging
}
}else
{
$sign = $str; //if only one sign left in str simply use this
$deb = "Last sign, SIGN: $sign , new STR: $str<crlf>"; Sub "_DEBUG"; //Debugging
}
if ($RandomizeCaseToo) //User-settings: randomize case or not?
{
$case = rand(0,1); //random: do an case change or not?
if ($case==1){ $sign = recase($sign, "invert");}
}
$out = "$out$sign$delim"; //collect output: OutArray+Sign+Delim
$count++; //increase count
}
//trim surrounding spaces and comas:
$out = regexreplace($out, "[ ,]*(\b.+\b)[ ,]*", "$1");
// === create output ===
//just use $out somewhere.
// would be N,a,T,s,h,d,i,S,M,o,b,R,e,l,u,h,o,d
// or oodaLiBR u D MSThSNeh
// depending on user-settings.
//-----------------------------------------------------------------
//--------create an test output:
$lenIn = strlen($IN);
$lenOut = strlen($out);
text RandomizeCaseToo is: $RandomizeCaseToo<crlf>
Delimiter is: "$delim"<crlf>
DropSpaces is: $DropSpaces<crlf 2>
Original string ($lenIn):<crlf>$IN<crlf 2>
Output is ($lenOut):<crlf>$out;
/////////////////////////////////////////////
//Debugging sub routine
//Usage: $deb = "Position: xyz, Var: myVar"; Sub "_DEBUG";
"_DEBUG"
global $deb;
writefile("C:\Temp\deb.txt", "<date yyyy-mm-dd hh:nn:ss> $deb<crlf>" ,a);
Debug output:
Code: Select all
2011-03-21 14:31:51 RAND 15, str: ThisShouldbeRaNdoM, len: 18
2011-03-21 14:31:51 RAND case else, SIGN: d , new STR: ThisShoulbeRaNdoM
2011-03-21 14:31:51 RAND 9, str: ThisShoulbeRaNdoM, len: 17
2011-03-21 14:31:51 RAND case else, SIGN: b , new STR: ThisShouleRaNdoM
2011-03-21 14:31:51 RAND 16, str: ThisShouleRaNdoM, len: 16
2011-03-21 14:31:51 RAND case len , SIGN: M , new STR: ThisShouleRaNdo
2011-03-21 14:31:51 RAND 5, str: ThisShouleRaNdo, len: 15
2011-03-21 14:31:51 RAND case else, SIGN: h , new STR: TisShouleRaNdo
2011-03-21 14:31:51 RAND 6, str: TisShouleRaNdo, len: 14
2011-03-21 14:31:51 RAND case else, SIGN: u , new STR: TisSholeRaNdo
2011-03-21 14:31:51 RAND 0, str: TisSholeRaNdo, len: 13
2011-03-21 14:31:51 RAND case 0 , SIGN: T , new STR: isSholeRaNdo
2011-03-21 14:31:51 RAND 11, str: isSholeRaNdo, len: 12
2011-03-21 14:31:51 RAND case else, SIGN: o , new STR: isShleRaNdo
2011-03-21 14:31:51 RAND 2, str: isShleRaNdo, len: 11
2011-03-21 14:31:51 RAND case else, SIGN: S , new STR: ishleRaNdo
2011-03-21 14:31:51 RAND 0, str: ishleRaNdo, len: 10
2011-03-21 14:31:51 RAND case 0 , SIGN: i , new STR: shleRaNdo
2011-03-21 14:31:52 RAND 8, str: shleRaNdo, len: 9
2011-03-21 14:31:52 RAND case else, SIGN: o , new STR: shleRaNd
2011-03-21 14:31:52 RAND 7, str: shleRaNd, len: 8
2011-03-21 14:31:52 RAND case else, SIGN: d , new STR: shleRaN
2011-03-21 14:31:52 RAND 3, str: shleRaN, len: 7
2011-03-21 14:31:52 RAND case else, SIGN: e , new STR: shlRaN
2011-03-21 14:31:52 RAND 3, str: shlRaN, len: 6
2011-03-21 14:31:52 RAND case else, SIGN: R , new STR: shlaN
2011-03-21 14:31:52 RAND 0, str: shlaN, len: 5
2011-03-21 14:31:52 RAND case 0 , SIGN: s , new STR: hlaN
2011-03-21 14:31:52 RAND 0, str: hlaN, len: 4
2011-03-21 14:31:52 RAND case 0 , SIGN: h , new STR: laN
2011-03-21 14:31:52 RAND 2, str: laN, len: 3
2011-03-21 14:31:52 RAND case else, SIGN: N , new STR: la
2011-03-21 14:31:52 RAND 0, str: la, len: 2
2011-03-21 14:31:53 RAND case 0 , SIGN: l , new STR: a
2011-03-21 14:31:53 Last sign, SIGN: a , new STR: a
Test output:
Code: Select all
RandomizeCaseToo is: 1
Delimiter is: ","
DropSpaces is: 1
Original string (21):
This Should be RaNdoM
Output is (35):
d,B,m,h,u,T,o,s,I,O,d,E,r,s,h,N,L,a
Many thanks to Don!
.