Results 1 to 7 of 7
  1. #1

    Checking if strings contain only alphanumeric characters plus space

    hi,

    I am a newbie in php. I know how in c++ ,you could cast a value to get its ascii value...

    Anyone know how to check if a string contain only case-insensitive alphabets/digits and spaces?

    thanks

  2. #2
    Join Date
    Apr 2003
    Location
    UK
    Posts
    2,560
    maybe using a regular expression using something like preg_match

    preg_match is meant to work in the same way as perls regular expressions, and in perl i can use something like:

    Code:
    #!/usr/local/bin/perl
    
    my $test1 = "abcDEF";
    my $test2 = "Ab de";
    my $test3 = "RFG %YU";
    
    checkstring($test1);
    checkstring($test2);
    checkstring($test3);
    
    sub checkstring {
      my ($string) = @_;
      if($string =~ /[^\w\s]/) {
        print "-$string- fail\n";
      } else {
        print "-$string- pass\n";
      }
    }
    which returns:

    -abcDEF- pass
    -Ab de- pass
    -RFG %YU- fail

  3. #3
    Join Date
    Apr 2000
    Location
    California
    Posts
    3,051
    Just a note, \w allows any word character, which includes an underscore. I don't know if that's relevant for your script to check. If not, use \w. If it can't contain an underscore, you can either specifically check for that as well, or just have the character class and replace \w with a-z with a case insensitive check, or a-zA-Z literally. There are other options as well, and the regular expression isn't coded like it is in Perl, though the regex check itself should be. Also, that will allow for new lines, which you probably don't want either.
    Last edited by Tim Greer; 06-28-2005 at 05:40 AM.

  4. #4
    thanks guys

  5. #5
    here is the regular expressions..

    if anyone need it

    PHP Code:
    if (!eregi("^[[:alnum:]]+$",$username)) { 
    Last edited by overulem; 06-28-2005 at 12:35 PM.

  6. #6
    anyway..

    any of you guys know any more of this?

    :alnum:

    thanks

  7. #7
    Join Date
    Dec 2003
    Location
    Earth
    Posts
    144
    Originally posted by overulem
    anyway..

    any of you guys know any more of this?

    :alnum:

    thanks
    [:alnum:] is a POSIX-style character class.

    e.g.

    alpha
    digit
    lower
    upper
    space...
    if($php !== $javascript){
    echo "Good it's not supposed to be";
    }

Posting Permissions

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