Page 1 of 1
how to remove duplicated strings?
Posted: 21 Aug 2011 16:18
by tiago
Hello, people!
I need a bit of help on this. Often I need to remove certain duplicated characters but only specific ones. Like in "fgg agg ---------g hhhhhh" I just need to reduce "---------" to "-".
Code: Select all
$a = "fgg agg ---------g hhhhhh"; $b = formatlist($a, d, ""); echo $b;
won't do because it eliminates also "gg" and "hs".
Code: Select all
$a = "fgg agg ---------g hhhhhh"; replace $a, $a, "--", "-"; echo $a;
leaves me with "-----" and I need some recursion here until I have just "-" or "--", sometimes.
As the size of repetitions vary I can't just do a
replace $a, $a, "---------", "-";
Please advice.
Re: how to remove duplicated strings?
Posted: 21 Aug 2011 16:54
by highend
zzZZzz
Code: Select all
global $Input, $LastPos, $Sign;
$Input = "fgg agg ---------g hhhhhh";
$Sign = "-"; sub "_LastPosition";
$FirstPos = strpos($Input, $Sign);
$Length = $LastPos - $FirstPos;
$FullString = strrepeat($Sign, $Length);
replace $Input, $Input, $FullString, $Sign; echo $Input;
"_LastPosition"
// "_LastPosition" gives last position of $Sign in $Input
global $Input, $LastPos, $Sign;
$Index=0;
while($Index < strlen($Input))
{
If ($Input=="") { break; }
$check = strpos($Input, "$Sign", strlen($Input) - $Index);
If ($check > -1) { $LastPos = $check +1; break; }
$Index++;
}
Re: how to remove duplicated strings?
Posted: 21 Aug 2011 17:06
by tiago
I thought I was dreaming when I thought there should be a simpler solution for this.
Highend's dream proves I was just dreaming.
Thank you Highend!

Re: how to remove duplicated strings?
Posted: 21 Aug 2011 17:17
by tiago
It's failing with "fgg ---- ---- agg ---------g hhhhhh" and "fgg agg ---------g hhhhhh -------".
Failing means: output == input! ::shocked::
Can't see a reason why.
Re: how to remove duplicated strings?
Posted: 21 Aug 2011 18:17
by highend
Ofc it fails with these input strings.
Did you ever mention that there would be more than one occurrence of --... in one line? Nope.
Just use a simple regex
Code: Select all
$Input = "...";
text regexreplace($Input, "(-+)", "-");
Re: how to remove duplicated strings?
Posted: 21 Aug 2011 18:57
by tiago
thank you, that worked as expected!
Re: how to remove duplicated strings?
Posted: 21 Aug 2011 19:08
by Stefan
tiago wrote:Often I need to remove certain duplicated characters but only specific ones.
Like in "fgg agg ---------g hhhhhh"
I just need to reduce "---------" to "-".
tiago wrote:It's failing with "fgg ---- ---- agg ---------g hhhhhh"
and "fgg agg ---------g hhhhhh -------".
Code: Select all
//Keeps first found $PattToFind in an row and drops all others:
$in = "fgg--- agg ---------g hhh---hhh";
$PattToFind = "-";
$PattFound=0; $loop=1; $out="";
while($loop < strlen($in)){
$CurrSign = substr($in, $loop, 1);
if ($Currsign==""){break;}
if ($CurrSign==$PattToFind){
if ($PattFound==0){$out=$out.$CurrSign; $PattFound=1;}
}else{
$out=$out.$CurrSign; $PattFound=0;
}
$loop++;
}
msg $in<crlf>$out;
Out:
Code: Select all
---------------------------
XYplorer
---------------------------
fgg--- agg ---------g hhh---hhh
gg- agg -g hhh-hhh
---------------------------
OK
---------------------------