how to remove duplicated strings?

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
tiago
Posts: 589
Joined: 14 Feb 2011 21:41

how to remove duplicated strings?

Post 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.
Power-hungry user!!!

highend
Posts: 14954
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: how to remove duplicated strings?

Post 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++;
		}
One of my scripts helped you out? Please donate via Paypal

tiago
Posts: 589
Joined: 14 Feb 2011 21:41

Re: how to remove duplicated strings?

Post 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! ;)
Power-hungry user!!!

tiago
Posts: 589
Joined: 14 Feb 2011 21:41

Re: how to remove duplicated strings?

Post 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.
Power-hungry user!!!

highend
Posts: 14954
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: how to remove duplicated strings?

Post 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, "(-+)", "-");
One of my scripts helped you out? Please donate via Paypal

tiago
Posts: 589
Joined: 14 Feb 2011 21:41

Re: how to remove duplicated strings?

Post by tiago »

thank you, that worked as expected!
Power-hungry user!!!

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: how to remove duplicated strings?

Post 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   
---------------------------

Post Reply