PDA

View Full Version : strfind won't work


Extasy4
13/08/2013, 05:57 PM
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?

Misiur
13/08/2013, 06:00 PM
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)

Extasy4
13/08/2013, 06:14 PM
Should I write this only at the very end? So after the last strfind?

Misiur
13/08/2013, 06:23 PM
I'd suggest something like:
#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!");
}

Extasy4
13/08/2013, 06:29 PM
That's nice, I'll try it:D

Extasy4
13/08/2013, 06:34 PM
But why did u write '!' in front of the forbidden words?

Misiur
15/08/2013, 01:06 PM
! 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

Extasy4
15/08/2013, 02:21 PM
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?

Extasy4
17/08/2013, 10:31 PM
bump

cessil
17/08/2013, 11:08 PM
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
http://wiki.sa-mp.com/wiki/Strfind