String management. Special chars!

Discussion in 'PHP' started by Justcrapx, Oct 29, 2006.

  1. Justcrapx

    Justcrapx New Member

    Joined:
    Oct 30, 2006
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    You have to use Regular Expressions (regex) to filter out bad characters.Checkout the exmaple below.

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

    Justcrapx New Member

    Joined:
    Oct 30, 2006
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  4. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    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?
     
  5. Justcrapx

    Justcrapx New Member

    Joined:
    Oct 30, 2006
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  6. Justcrapx

    Justcrapx New Member

    Joined:
    Oct 30, 2006
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    What i exactly want is to make users chose their nicknames whic will only match the pattern i set.
     
  7. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    PHP:
    $string "Asdsd - sdsd ";
     if(!
    preg_match("/^[A-Za-z0-9-._]+$/",$string)) //see here I allowed -,_ and .
     
    {
        print(
    "Bad characters found!");
     }
     
  8. Justcrapx

    Justcrapx New Member

    Joined:
    Oct 30, 2006
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Wow, i guess i now got it. Thank you very much pradeep. I love php =).
     
  9. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Me too! Keep posting, about your problems discoveries and inventions.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice