strfind won't work
#1

Hi!

I have this:

Код:
			if(strfind(inputtext, "0", true) || strfind(inputtext, "1", true) || strfind(inputtext, "2", true) || strfind(inputtext, "3", true) || strfind(inputtext, "4", true) || strfind(inputtext, "5", true) || strfind(inputtext, "6", true) || strfind(inputtext, "7", true) || strfind(inputtext, "8", true) || strfind(inputtext, "9", true) ||
   			strfind(inputtext, "й", true) || strfind(inputtext, "у", true) || strfind(inputtext, "ц", true) || strfind(inputtext, "ő", true) || strfind(inputtext, "н", true) || strfind(inputtext, "ъ", true) || strfind(inputtext, "ь", true) || strfind(inputtext, "ű", true) || strfind(inputtext, "б", true) ||
   			strfind(inputtext, "hitler", true) || strfind(inputtext, "rambo", true) || strfind(inputtext, "buzi", true) || strfind(inputtext, "kocsog", true) || strfind(inputtext, "geci", true) || strfind(inputtext, "fasz", true) || strfind(inputtext, "fos", true) || strfind(inputtext, "shit", true) || strfind(inputtext, "fuck", true))
		 	{
				SendClientMessage(playerid, COLOR_TOMATO, "Error: The text cannot contain special characters!");
		 	}
		 	else
			{
	 		        SendClientMessage(playerid, COLOR_WHITE, "You're cool");
	 		}
But whenever I type anything in the field the error message pops up. Even if I write only an 'a' letter..

Can anyone help please?
Reply
#2

Strfind returns -1 on failure, not 0, so when you do if(strfind(a, "b", true)) it will return true even for -1. You need to use (strfind(a, "b", true) != -1)
Reply
#3

Should I write this only at the very end? So after the last strfind?
Reply
#4

I'd suggest something like:
pawn Код:
#define N_CENSORED 2 //number of censored words
#define N_CENSORED_LEN 32 //length of max string
static const Censored[N_CENSORED_LEN][(N_CENSORED_LEN + 1) char] = {
    !"Hitler",
    !"Pancakes"
};

stock ContainsCensored(const input[]) {
    for(new i = 0; i != N_CENSORED; ++i) {
        if(strfind(input, Censored[i], true) != -1)  return 1;
    }
    return 0;
}

//...
if(ContainsCensored(inputtext)) {
    SendClientMessage(playerid, COLOR_TOMATO, "Error: The text cannot contain swearwords/some characters!");
}
Reply
#5

That's nice, I'll try it
Reply
#6

But why did u write '!' in front of the forbidden words?
Reply
#7

! is for packed strings, so their size is 4 times smaller in memory. Strfind can work with packed strings, however not every function can (you have to use strunpack then). You should store big arrays of strings in packed form, and then strunpack them on fly
Reply
#8

Wow, thank you so much! It's finally working now. I have one last question: What if I use '!strfind'? what value should I write at the end?
Reply
#9

bump
Reply
#10

if you use !strfind then you're checking if strfind returns 0
if you want to check if a string doesn't contain a string you should check for it returning -1
https://sampwiki.blast.hk/wiki/Strfind
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)