Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/forums/php/)
-   -   String management. Special chars! (http://www.go4expert.com/forums/string-management-special-chars-t1752/)

Justcrapx 30Oct2006 04:46

String management. Special chars!
 
I was wondering how to make sure that there are no special chars included in a string like " ^ $ # " which i dont want people to use in their nicknames. In other words, how can i make sure that they only use english characters and numbers. I have tried to figure it out on PHP documentation but appears to be too complex for a beginner.

pradeep 30Oct2006 11:23

Re: String management. Special chars!
 
You have to use Regular Expressions (regex) to filter out bad characters.Checkout the exmaple below.

Code: PHP

$string = "Asdsd - sdsd ";
if(!preg_match("/^[\w]+$/",$string))
{
   print("Bad characters found!");
}


Justcrapx 30Oct2006 14:58

Re: String management. Special chars!
 
Thanks alot! That "regex" syntax is really lookin hard. Hope i can figure it out. There are so many characters to be omitted, so working out the pattern will be something messy i guess.

pradeep 30Oct2006 15:09

Re: String management. Special chars!
 
That regex only allows alphabets and numbers, do you want to remove the bad characters or just inform the user that he has enter some bad characters in his/her nickname?

Justcrapx 30Oct2006 15:09

Re: String management. Special chars!
 
I just noticed that the example pattern you've given filters out the entire charz except letters and numbers. Now the question is how to ommit numbers and allow some spesific charz.

Justcrapx 30Oct2006 15:11

Re: String management. Special chars!
 
What i exactly want is to make users chose their nicknames whic will only match the pattern i set.

pradeep 30Oct2006 15:11

Re: String management. Special chars!
 
Code: PHP

$string = "Asdsd - sdsd ";
 if(!preg_match("/^[A-Za-z0-9-._]+$/",$string)) //see here I allowed -,_ and .
 {
    print("Bad characters found!");
 }


Justcrapx 30Oct2006 15:16

Re: String management. Special chars!
 
Wow, i guess i now got it. Thank you very much pradeep. I love php =).

pradeep 30Oct2006 15:24

Re: String management. Special chars!
 
Me too! Keep posting, about your problems discoveries and inventions.


All times are GMT +5.5. The time now is 18:44.