Re: Randomness
Posted: 22 Mar 2011 09:18
Many thanks Don, works just fine.admin wrote:OK; I already did it as you hay have seen.
I have updated the script for tiago at http://www.xyplorer.com/xyfc/viewtopic. ... 721#p57721
with this new functions to test how they works.
-
The extended replace() function i have tested some more to show what can be done and if it is complete.
An option to
1) replace only occurrence number n (in addition to the first n occs)
and
2) replace only just the last occ.
would be nice.
But i have seen this can done by scripting:
Here are a few examples just to show i use this new options (and how)
//Syntax: replace(string, search, replacement, [matchcase], [start=1], [count=-1]))
//Syntax: replace(string, search, replacement, [matchcase: 0=Yes;1=No ], [start=1], [count=-1: -1=All; 1=One repl.; 2=Two repl. ]))
Code: Select all
//replace first upper case S
$IN = "This Should be RaNdoM";
$str = replace($IN, "S", "", 1, 1, 1); msg $str;
//This hould be RaNdoM
//replace first s, no matter what case
$IN = "This Should be RaNdoM";
$str = replace($IN, "S", "", 0, 1, 1); msg $str;
//Thi Should be RaNdoM
//replace an upper case S after pos 14
$IN = "This Should be RaNdoMneS";
$str = replace($IN, "S", "", 0, 14, 1); msg $str;
//This Should be RaNdoMne
//replace all S, no matter what case, but only after pos 5 / skip first chars
$IN = "This Should be RaNdoMneS";
$str = replace($IN, "S", "", 0, 5, -1); msg $str;
//This hould be RaNdoMne
//replace all S, no matter what case
$IN = "This Should be RaNdoMneS";
$str = replace($IN, "S", "", 0, 1, -1); msg $str;
//Thi hould be RaNdoMne
//replace first three 'e', case insensitive
$IN = "There Should be RaNdoMneSess";
$str = replace($IN, "e", "", 0, 1, 3); msg $str;
//Thr Should b RaNdoMneSessCode: Select all
//replace the first n occurrences only (just for fun, the function can do it also):
$count=0;
$IN = "There Should be RaNdoMneSess";
while( strpos( $IN, "e") != -1)
{
$count++;
$IN = replace($IN, "e", "", 0, 1, 1);
if ($count > 2) {break;}
}
msg $count $IN;
//3 Thr Should b RaNdoMneSessCode: Select all
// replace n'th occurrence of an sign
// === User Settings ===
$IN = "There Should - be - RaNdoMneSess";
$SignToReplace="-";
$ReplaceWith = "";
$OccurrenceToReplace = 2;
// === Don't modify code below ===
//---count the parts, delimiter-ed by $SignToReplace---
$occu = gettoken($IN, "count", $SignToReplace);
if ($occu > 0)
{
//--find the pos of the n'th occurrence--
$count=0;
$pos=0;
while($count < $OccurrenceToReplace)
{
//strpos(haystack, needle, [start=0], [matchcase])
$pos = strpos($IN, $SignToReplace, $pos +1);
$count++;
}
//Syntax: replace(string, search, replacement, [matchcase: 0=Yes;1=No ], [start=1], [count=-1: -1=All; 1=One repl.; 2=Two repl. ]))
$out = replace($IN, $SignToReplace, $ReplaceWith, 0, $pos, 1);
msg "$IN<crlf>SignToReplace: $SignToReplace<crlf>Repl. occu #: $OccurrenceToReplace<crlf>Pos: $pos<crlf>$out";
}
//> There Should - be - RaNdoMneSess
//> SignToReplace: -
//> Repl. occu #: 2
//> Pos: 18
//> There Should - be RaNdoMneSessCode: Select all
// replace all but last occurrence of an sign
// === User Settings ===
$IN = "There.Should.be_-_RaNdoM.neSess.ext";
$SignToReplace=".";
$ReplaceWith = " ";
// === Don't modify code below ===
$occus = gettoken($IN, "count", $SignToReplace);
if ($occus > 0)
{
//Syntax: replace(string, search, replacement, [matchcase: 0=Yes;1=No ], [start=1], [count=-1]))
$out = replace($IN, $SignToReplace, $ReplaceWith, 0, 1, $occus -2);
msg "$IN<crlf>SignToReplace: $SignToReplace<crlf>ReplaceWith: $ReplaceWith<crlf>Occurences: $occus<crlf 2>$out";
}
//> There.Should.be_-_RaNdoM.neSess.ext
//> SignToReplace: .
//> ReplaceWith:
//> Occurences: 5 (bug? no, see below)
//> There Should be_-_RaNdoM neSess.ext
/*
Occurences: 5 bug? no, gettoken's "count" counts the parts, not the delimiters.
So if i had remembered this sooner i had perhaps should use smtg like:
$PARTS = gettoken($IN, "count", $SignToReplace);
$out = replace($IN, $SignToReplace, $ReplaceWith, 0, 1, $PARTS -2);
*/
Code: Select all
// replace only the last occurrence of an sign
// === User Settings ===
$IN = "There Should - be - RaNdoM-neSess.ext";
$SignToReplace="-";
$ReplaceWith = "#";
// === Don't modify code below ===
$occus = gettoken($IN, "count", $SignToReplace);
if ($occus > 0)
{
$count=0;
$pos=0;
while($count < $occus -1)
{
//strpos(haystack, needle, [start=0], [matchcase])
$pos = strpos($IN, $SignToReplace, $pos +1);
$count++;
}
//Syntax: replace(string, search, replacement, [matchcase: 0=Yes;1=No ], [start=1], [count=-1: -1=All; 1=One repl.; 2=Two repl. ]))
$out = replace($IN, $SignToReplace, $ReplaceWith, 0, $pos, 1);
msg "$IN<crlf>SignToReplace: $SignToReplace<crlf>Occus: $occus<crlf>Pos: $pos<crlf>$out";
}
//> There Should - be - RaNdoM-neSess.ext
//> SignToReplace: -
//> Occus: 4
//> Pos: 26
//> There Should - be - RaNdoM#neSess.ext