Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Non alphanumeric regex - easy!

Posted on 2004-11-07
9
Medium Priority
?
649 Views
Last Modified: 2008-01-16
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?
0
Comment
Question by:thepadders
9 Comments
 
LVL 27

Expert Comment

by:Diablo84
ID: 12519609
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
 
LVL 27

Assisted Solution

by:Diablo84
Diablo84 earned 400 total points
ID: 12519615
a better pattern to use is:

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

which will cover both cases
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12519618
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 63

Accepted Solution

by:
Zvonko earned 600 total points
ID: 12519663
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
 
LVL 27

Expert Comment

by:Diablo84
ID: 12519704
good point about \w, it of course covers _ too

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

Expert Comment

by:eeBlueShadow
ID: 12519954
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
 
LVL 1

Expert Comment

by:mces
ID: 12521746
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
 

Author Comment

by:thepadders
ID: 12522504
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 12522570
You are welcome.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question