Check character then replace
I want to only allow A-Z, 0-9, and a few special characters like [](),.-~#=+-, on nicknames.
PHP Code:
This is a test i did, its only for the first character: PHP Code:
|
Re: Check character then replace
It's not 2d array, because you have characters. If you had strings for example instead of "a" you had "alphabeta" then you should use double [][] but here is only [].
PHP Code:
PHP Code:
if(containi(g_AllowNickCharacter,szNewName[i]) == -1) it means that if current symbol of player's name doesnt exists in allowed constant array then this character will removed with your method. |
Re: Check character then replace
Quote:
|
Re: Check character then replace
bump
|
Re: Check character then replace
When using double quotes ( " ), you're basically telling the compiler that it should terminate the string after each quote So the string is actually:
a, null, b, null ... You should use single quotes ( ' ) for characters. Code:
Code:
a, a, aCode:
Code:
abc, abc, aRemember though, you have to terminate the string by putting null at the end or all functions will continue reading memory until it reaches 0, which could cause memory access runtime errors. Code:
Code:
abcTest Plugin 5, abcTest Plugin 5, aCode:
Code:
abc, abc, aCode:
Code:
replace() will check for occurances, skip containi() and add upper case letters instead. With all that said, you should check out an ASCII chart and try using direct character comparisons instead. For example: Code:
Code:
Code:
unsorted: abcdefghijklmnopqrstuvxz<>,+*'?=)(/#.-;:_~!|\[]1234567890@Code:
/*This way is the most efficient. There are a lot of comparisons, but replace inside a loop is loop inception. Don't get me started on replace_all. loop3 Code:
Code:
string1: abc+Ñ+ñ+ÂABC-ñ$123Ôé¼%!Well. Comparing a variable 12 times will make it read the value of that variable from the memory 12 times. With a simple switch(), you can reduce that to 1! Code:
Code:
string1: abc+Ñ+ñ+ÂABC-ñ$123Ôé¼%!In the end, none of these methods are slow. There's no need to worry about using one over another. |
Re: Check character then replace
Quote:
Thank you for your detailed answer, it works correctly. PS: You forgot to add PHP Code:
|
Re: Check character then replace
Why checking all allowed symbols if you can check only symbols which are not allowed?
PHP Code:
|
Re: Check character then replace
Quote:
Code:
Also, do not hard code chars: Code:
Code:
Code:
Code:
@Black Rose solves well the problem, if you want to invert things, do it over his algorithm inverting its logic. However, the performance on this case is kinda irrelevant whether you check the allowed symbols or the not allowed symbols. |
Re: Check character then replace
Quote:
There are way to many: http://image.prntscr.com/image/676b4...13c69876f7.png Anyways the @Black Rose method works perfectly, its exactly what i wanted. |
| All times are GMT -4. The time now is 18:36. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.