Author Topic: strReplace  (Read 1095 times)

Is there a way to make it ignore caps without using strLwr on either the word it's lookng for or the string it's looking in?

stricmp() will compare two strings with case insensitivity.

However, strlwr() only affect your original string if you do %string = strlwr(%string).  Using the following will not affect your original value.

Code: [Select]
%needle = "Test";
%haystack = "Testing";

strstr(strlwr(%haystack),strlwr(%needle));

I need to replace a word no matter it's caps for the word filter.

Example:
Code: [Select]
//The word we're looking for is "Legoland"
//The word we're replacing it with is "Blockland"

%message = "I think legoland is the BEST!";
%badword = "Legoland";
%replacement = "Blockland";

//We could do this:
%message = strReplace(strLwr(%message),strLwr(%badword),%replacement);


//But then the message will come out as "i think Blockland is the best!"

I'm coding the way I recommend doing this now.  I'll PM you when done.


You could strlwr the message and find the position of the badword, replace the word in the un lowercase by using the position and length of the badword.
Code: [Select]
%badword = " NOOB ";
%replacement = " im a cigarette! "
%ltext = strlwr(%txt);
while(strstr(%ltext,strlwr(%badword))!= -1){
%ltext = strlwr(%txt);
%pos = %strpos(%ltext,strlwr(%badword));
if(%pos != -1){
%t1=getsubstring(%txt,0,%pos-1);
%t2=getsubstring(%txt,%pos+strlen(ltrim(rtrim(%badword))));
%txt = %t1 @ %replacement @ %t2;
}
}

Meh, I've got a much better solution for this.  Plus, you never defined %txt above.  I'm still working on an issue or two, but my code will allow for proper treatment of capitalized and lowercase letters and words with neighboring symbols (e.g. !lag! could become !my computer is slow!).

%txt is to be whatever your searching in.

It's OK.  I already submitted the new code.  You can pick up a copy of WordFilter v3 that supports replacing words with multiple words and retaining any symbols used (!@#$, etc) here:

http://www.blockland.us/smf/index.php?topic=27441.0