Non alphanumeric regex - easy!

I have a simple regex question, I just want to know if a string contains any characters other than a-z, 0-9 and - and _

I tried this:

      if (preg_match("/[^A-Z0-9_-]/", $_REQUEST['name'])) {
            echo "here";
      }

but that is not right. What am I doing wrong?
thepaddersAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Diablo84Commented:
hi thepadders,

try this pattern: "/^[A-Z0-9_-]+$/"

     if (preg_match("/^[A-Z0-9_-]+$/", $_REQUEST['name'])) {
          echo "here";
     }

bear in mind it is case sensitive
0
Diablo84Commented:
a better pattern to use is:

"/^[\w\d_-]+$/"

which will cover both cases
0
Diablo84Commented:
This page may be useful to you: http://us2.php.net/manual/en/reference.pcre.pattern.syntax.php

Pattern syntax. Note in the above i have used:

\w =  any "word" character
\d =  any decimal digit
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

ZvonkoSystems architectCommented:
I would reduce it to this:


     if (preg_match("/[^\w\-]/", $_REQUEST['name'])) {
          echo "here";
     }

Because \d is contained in \w and - without \- indicates a range in square braces.

Also a field with the name name is slightly confusing ;-)

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Diablo84Commented:
good point about \w, it of course covers _ too

although your pattern does not match with a test, so perhaps the pattern: "/^[\w\-]+$/"
0
eeBlueShadowCommented:
Zvonko,

a '-' placed at the start or end of a character class [square braces] does not need escaping, only when it is surrounded by other characters :)

But that's as good as it gets

_Blue
0
mcesCommented:
Diablo, what you recommend, matches to strings that ALL THEIR CHARACTERS are something other than alphanumberic and dash.
The pattern he looks for is /[^\w-]/.  Just that.
0
thepaddersAuthor Commented:
Thank you everyone, mces had the best answer is    

 if (preg_match("/[^\w\-]/", $_REQUEST['name'])) {
          echo "here";
    }

I have split the points as it was a progression to that answer. Thanks everyone.
0
ZvonkoSystems architectCommented:
You are welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.