Author Topic: How to filter and replace a chat segment  (Read 1562 times)

stripos. Case insensitive strpos. I just checked it in the console, and it exists.

-snip-

Dude, you just basically reposted Truce's first code. I asked him, and he made an even better version that accounts for making case changes to text with it. Why did you post that?

I didn't notice that one.

In that case:

Code: [Select]
function striReplace(%source, %search, %replace)
{
    if(strlen(%search) < 1)
      return;
    %len = strlen(%search);
    %i = 0;
    while((%i = stripos(%source, %search, %i)) >= 0)
    {
        %source = getSubStr(%source, 0, %i) @ %replace @ getSubStr(%source, %i + %len, strlen(%source) - %i - %len);
        %i += strlen(%replace);
    }
    return %source;
}

Make use of that optional third argument!

(Edit: input check)
« Last Edit: November 29, 2010, 01:57:39 PM by Uristqwerty »

Isn't there a function in torque to convert a string to lowercase?

Assigning the text to a variable, converting it to lowercase, then checking it for the word = possible solution?

I know this has been solved already, I just thought this was another way.
« Last Edit: November 30, 2010, 03:49:16 AM by Miles Barlow »

Isn't there a function in torque to convert a string to lowercase?

Assigning the text to a variable, converting it to lowercase, then checking it for the word = possible solution?

I know this has been solved already, I just thought this was another way.
Yes there is, but there really isn't a way to get that case back after you've done your replacements.
So if this was filtering chat, then all chat would have to be lowercase if you did it that way.

Yes there is, but there really isn't a way to get that case back after you've done your replacements.
So if this was filtering chat, then all chat would have to be lowercase if you did it that way.
Eh, didn't think about that  :cookieMonster:

I didn't notice that one.

In that case:

Code: [Select]
function striReplace(%source, %search, %replace)
{
    if(strlen(%search) < 1)
      return;
    %len = strlen(%search);
    %i = 0;
    while((%i = stripos(%source, %search, %i)) >= 0)
    {
        %source = getSubStr(%source, 0, %i) @ %replace @ getSubStr(%source, %i + %len, strlen(%source) - %i - %len);
        %i += strlen(%replace);
    }
    return %source;
}

Make use of that optional third argument!

(Edit: input check)
Is that case-sensitive?

EDIT: That script isn't working, why do your scripts always break!

Here's the code to my chat filter
Code: [Select]
function striReplace(%source, %search, %replace)
    {
    if(strlen(%search) < 1)
      return;
    %len = strlen(%search);
    %i = 0;
    while((%i = stripos(%source, %search, %i)) >= 0)
    {
        %source = getSubStr(%source, 0, %i) @ %replace @ getSubStr(%source, %i + %len, strlen(%source) - %i - %len);
        %i += strlen(%replace);
    }
    return %source;
}
package FroPack_ChatStuff
{
function ChatSound()
{
   for(%i=0; %i < ClientGroup.getCount(); %i++) {
      %cl = ClientGroup.getObject(%i);
     
      %cl.play2D(Sawtooth);
   }   
}
     function serverCmdMessageSent(%cl,%msg)
     {
          %msg = striReplace(%msg,"rotten frog","morsch Frosch"); //begin tenda-to-german conversions
          %msg = striReplace(%msg,"frying pan","Bratpfanne");
          %msg = striReplace(%msg,"yiff","983d909713cf1feb33ee821c1915e0ae"); //begin furryspeak-to-MD5 conversions
          %msg = striReplace(%msg,"vore","03b45bce682586cafb346f27e076d97b");
          %msg = striReplace(%msg,"furcigarette","4e38bbe5e0ec1d0709f9c283a7b1d195");
          %msg = striReplace(%msg,"furry","f96af09d8bd35393a14c456e2ab990b6");
          %msg = striReplace(%msg,"furries","1ff7f8b0b702e9de1b1e6a72e4550361");
          Parent::serverCmdMessageSent(%cl,%msg);
          ChatSound();
     }
};
activatepackage(FroPack_ChatStuff);
datablock AudioProfile(Sawtooth)
{
   filename = "./Chat.wav";
   description = AudioClose3d;
   is3D = true;
preload = true;
};
« Last Edit: December 05, 2010, 01:45:26 PM by frogger3140 »

Use this one, it's from Truce:
Ok, fine. Complication for case changing go:

Code: [Select]
function striReplace(%source,%search,%replace)
{
if(%search $= "")
return %source;

%lena = strLen(%source);
%lenb = strLen(%search);

for(%i = 0; %i < %lena; %i++)
{
%part = getSubStr(%source,%i,%lenb);

if(%part !$= %search)
{
%clean = %clean @ getSubStr(%source,%i,1);
continue;
}

%clean = %clean @ %replace;
%i    += %lenb - 1;
}

return %clean;
}