Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2008
    Location
    Vancouver, Canada
    Posts
    653

    Question Regex and preg_match() issues

    I am currently working on a script for a client and I am having some issues with preg_match(). I am trying to validate the form field to prevent the use of invalid characters (characters that are not numerical or alphabetical).

    Sample code...
    Code:
    <?php
    if(!preg_match('/[a-zA-Z0-9]/',$somestring)){
    	print "ok";
    } else {
    	print "not ok";
    }
    ?>
    It works if the invalid character is in the beginning of the string:
    Code:
    $somestring = "!@hello";
    But if it isn't at the beginning, it doesn't work:
    Code:
    $somestring = "hello!@";
    Does anyone know how to properly do the regex?

    Thanks,
    Jay
    Tailored VPS offers fully customizable VPS Hosting
    Powered by OpenVZ | Servers located in the USA | 99.9% Uptime

  2. #2
    Join Date
    Feb 2005
    Location
    Australia
    Posts
    5,849
    You're looking for a word character in the string but you really want to look for a non-word character. Perl regexps have the shortcuts \w for [a-zA-Z0-9] and \W for anything not in that set (non-word characters). Try this:
    PHP Code:
    <?php
    if(preg_match('/\W/',$somestring)){
        print 
    "not ok";
    } else {
        print 
    "ok";
    }
    ?>
    Chris

    "Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them." - Laurence J. Peter

  3. #3
    Join Date
    Nov 2001
    Location
    Vancouver
    Posts
    2,422
    I also agree that using your regex library's "word" metacharacter is a *much* better choice, in the long run. It is shorter than a character class to type and thus perhaps less prone to typos, but more importantly what defines a "word" will vary from one language to another. A simplistic character class a-Z isn't going to cut it if a user quite legitimately enters an accented or non ASCII letter in one of your form fields. Future proof your regexes that might have to deal with Unicode / non ASCII input.

    But to answer the OP's question, why didn't it work? Your test wasn't considering the entire string and thus would never see the invalid characters at the end of the string, or anywhere but the first character. Change your code to: /^[a-zA-Z0-9]+$/ and you'll find it does what you expected.

    ps: an on-line regex tester that knows about pcre might be useful - here's one from a Google search: http://www.quanetic.com/Regex
    “Even those who arrange and design shrubberies are under
    considerable economic stress at this period in history.”

  4. #4
    Join Date
    Aug 2008
    Location
    Vancouver, Canada
    Posts
    653
    Thanks for the advice! The script is now working properly.
    Do you know any websites that has tutorials on Regex?

    Thanks again,
    Jay
    Tailored VPS offers fully customizable VPS Hosting
    Powered by OpenVZ | Servers located in the USA | 99.9% Uptime

  5. #5
    Join Date
    Sep 2006
    Location
    Cardiff - United Kingdom
    Posts
    1,569
    Quote Originally Posted by AHN-Jay View Post
    Thanks for the advice! The script is now working properly.
    Do you know any websites that has tutorials on Regex?

    Thanks again,
    Jay
    Hi Jay,
    I found this a pretty good guide:

    http://www.phpro.org/tutorials/Intro...PHP-Regex.html

    In-fact, it's the best I've come across thus far

    Thanks,
    Tristan
    Plagiarism Guard - Protect Against Content Theft
    Tristan Perry - Personal blog

  6. #6
    Join Date
    Aug 2008
    Location
    Vancouver, Canada
    Posts
    653
    Quote Originally Posted by DH - Tristan Perry View Post
    Hi Jay,
    I found this a pretty good guide:

    http://www.phpro.org/tutorials/Intro...PHP-Regex.html

    In-fact, it's the best I've come across thus far

    Thanks,
    Tristan
    Thanks Tristan, that will come in handy
    Tailored VPS offers fully customizable VPS Hosting
    Powered by OpenVZ | Servers located in the USA | 99.9% Uptime

Similar Threads

  1. Replies: 2
    Last Post: 03-30-2006, 05:10 PM
  2. PHP: preg_match
    By fozzy in forum Programming Discussion
    Replies: 10
    Last Post: 06-30-2005, 02:40 PM
  3. php preg_match
    By getbusy in forum Programming Discussion
    Replies: 8
    Last Post: 09-14-2004, 06:18 PM
  4. preg_match(); help please!!
    By fkdean in forum Programming Discussion
    Replies: 1
    Last Post: 05-13-2004, 04:29 AM
  5. preg_match
    By GaleForce in forum Programming Discussion
    Replies: 3
    Last Post: 02-29-2004, 02:35 PM

Posting Permissions

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