SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict cranjled's Avatar
    Join Date
    Apr 2004
    Location
    ny
    Posts
    382
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Amending pattern to include certain special characters?

    I have been using the pattern ~\W+~ to remove all alphanumerics from a given string, as in:

    $arg = preg_replace('~\W+~', '', $arg);

    I would like to amend this so that it could be used to validate a couple of inputs that will accept arbitrary database username and password credentials.

    Is there an elegant pattern that will allow the various special characters that might appear in a database username/password combo?

    Thanks!

    Cranjled

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Location
    Swindon, UK
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about using ^ as a negate character?

    so, your function would look like:
    Code:
    $arg = preg_replace('[^_\-\w]', '', $arg); # Would replace any caracter except  _ - and alphanumerics with nothing.
    Just add special characters as you require them to the class

  3. #3
    SitePoint Addict cranjled's Avatar
    Join Date
    Apr 2004
    Location
    ny
    Posts
    382
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jazzer01 View Post
    How about using ^ as a negate character...Would replace any caracter except _ - and alphanumerics with nothing.
    That's what my original snip already does... I want to support alphanumerics as well as special characters.

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Location
    Swindon, UK
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, what caracters do you want to support?
    For example
    Code:
    $arg = preg_replace('[^&#%$_\-\w]', '', $arg);
    will support &,#,%,$,_,-, as well as alphas - all you need to do is add whatever characters you need to support into that regex

  5. #5
    SitePoint Addict cranjled's Avatar
    Join Date
    Apr 2004
    Location
    ny
    Posts
    382
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your continued support.

    The goal is to allow certain special characters that a person might use in a username/password pair, so the pattern you've demonstrated is pretty close, if not exactly, what it should achieve.

    Thanks!

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Location
    Swindon, UK
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No problem - hopefully you should be able to adapt that regex for whatever characters you want to use - like I said, I don't know what you want your restrictions on characters to be - but you do!

    Let me know if you need any more help


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •