Problem str_replace
#1

I want to forbid this character in the chat, I'm using this code that's not working; can someone help me?
Where do I have to put it to make it working?
PHP код:
stock strreplace(string[], findreplace)
{
    for(new 
i=0string[i]; i++)
    {
        if(
string[i] == find)
        {
            
string[i] = replace;
        }
    }
}
stock GetName(playerid)
{
    new 
name[24];
    
GetPlayerName(playeridnamesizeof(name));
    
strmid(str,name,0,strlen(name),24);
    for(new 
0MAX_PLAYER_NAMEi++)
    {
        if (
str[i] == '_'str[i] = ' ';
    }
    return 
str;

Reply
#2

You want to remove the "_" from the name?

To do this, you could also use:

pawn Код:
RemoveUnderScore(playerid)
{
    new userName[MAX_PLAYER_NAME];GetPlayerName(playerid,userName,sizeof(userName));
    for(new i = 0; i < MAX_PLAYER_NAME; i++)if(userName[i] == '_') userName[i] = ' ';
    return userName;
}
Here is an alternative to str_replace:

pawn Код:
stock strreplace(string[], const search[], const replacement[], bool:ignorecase = false, pos = 0, limit = -1, maxlength = sizeof(string)) {
    if (limit == 0)
        return 0;
   
    new sublen = strlen(search), replen = strlen(replacement), bool:packed = ispacked(string), maxlen = maxlength, len = strlen(string), count = 0;    
   
    if (packed)maxlen *= 4;
   
    if (!sublen)return 0;
   
    while (-1 != (pos = strfind(string, search, ignorecase, pos))) {
        strdel(string, pos, pos + sublen);        
        len -= sublen;        
        if (replen && len + replen < maxlen) {
            strins(string, replacement, pos, maxlength);
           
            pos += replen;
            len += replen;
        }
       
        if (limit != -1 && ++count >= limit)break;
    }
   
    return count;
}
Reply
#3

warning 203: symbol is never used: "RemoveUnderScore"

why?
Reply
#4

Quote:
Originally Posted by blanic
Посмотреть сообщение
warning 203: symbol is never used: "RemoveUnderScore"

why?
You need to actually use it. For instance:

pawn Код:
public OnPlayerConnect(playerid){
    new string[50];
    format(string, sizeof(string), "Welcome, %s.", RemoveUnderScore(playerid));
    SendClientMessage(playerid, -1, string);
    return 1;
}
You must replace all of your "GetName" with "RemoveUnderScore", or to make it easier, rename the function "RemoveUnderScore" to "GetName" and remove your older one.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)