SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with preg_match

    Hi there..

    I really need help with my ugly regex code because it still can't diferrentiate string and string contain only numbers ok here the scenario;

    All the string must meet this 3 conditions

    1.String contain only alpha,space and dot
    2.String contain only alphanumeric,space and dot
    3.String contain only numbers

    so here's my attempt code so far

    PHP Code:


    $testStringArray 
    = array("abc. bc. def83","abc. bc.","123","567","fdg. qwe.","dfg. rtu. hu8");

    //so i start filter testing here

    $total count($testStringArray);

    for(
    $i=0;$i<$total;$i++){
       if(
    preg_match('/[a-zA-Z.\...\\s.]+$/',$testStringArray[$i])){
                echo 
    $i." : ".$testStringArray[$i]." contain only alpha,dot,space";
      }else if(
    preg_match('/^[a-zA-Z0-9.\...\\s.]+$/i/',$testStringArray[$i])){
               echo 
    $i." : ".$testStringArray[$i]." contain only alpha,dot,numbers,space";
      }else if(
    preg_match('/^[0-9]/',$testStringArray[$i])){
          echo 
    $i." : ".$testStringArray[$i]." contain only numbers";
      }


    Current output:


    contain only alpha,dot,numbers,space
    contain only alpha,dot,space
    contain only alpha,dot,numbers,space-->it seems the regex not wokr here
    contain only alpha,dot,numbers,space-->it seems the regex not wokr here
    contain only alpha,dot,space
    contain only alpha,dot,numbers,space

    suppose to :

    contain only alpha,dot,numbers,space
    contain only alpha,dot,space
    contain only numbers--->shoud be numbers only
    contain only numbers--->shoud be numbers only
    contain only alpha,dot,space
    contain only alpha,dot,numbers,space

    So i really need help with it....thanks in advance guys...

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,083
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    You need to check /[0-9]/ before you check /^[a-zA-Z0-9.\...\\s.]+$/i/

    Because everything that matches /[0-9]/ will also match /^[a-zA-Z0-9.\...\\s.]+$/i/ and therefore get matched in the second if()
    The other way around is not true, so you should check if a string consists of only digits before you check if it consists of digits among others.

    BTW. No offence, but your regexes are weird.
    Why dont you use:

    Code:
    /^[a-zA-Z\.\s]+$/
    
    instead of
    
    /[a-zA-Z.\...\\s.]+$/
    Code:
    /^[a-zA-Z\d\.\s/]+$/
    
    instead of
    
    ^[a-zA-Z0-9.\...\\s.]+$/i/
    Note that \d = [0-9], and /i (making the regex case insensitive) is not needed since you already check for uppercase and lowercase chars

    Code:
    /^[0-9]$/
    
    instead of
    
    /^[0-9]$/
    You forgot the $ at the end
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


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
  •