how to check if a variable contains both numbers and letters ?

Discussion in 'PHP' started by ramysarwat, May 29, 2010.

  1. #1
    how to check if a variable contains both numbers and letters ?

    for example abcd1234

    and how to split numbers from letters in this example ?
     
    ramysarwat, May 29, 2010 IP
  2. kajol

    kajol Well-Known Member

    Messages:
    523
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Go with RegExp...
     
    kajol, May 29, 2010 IP
  3. Earnsite

    Earnsite Peon

    Messages:
    11
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Could do:
    $numtest = NULL;
    $lettertest = NULL;
    $array1 = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
    $array2 = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");
    $numtest = substr_count($string, $array1);
    $lettertest = substr_count($string, $array2);
    if($numtest == NULL || $lettertest == NULL){
    You're missing either a number or a letter.
    }

    Not sure if there's a btter way, not had to do that before.
     
    Earnsite, May 29, 2010 IP
  4. math.fox

    math.fox Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    i think if u used intval will give you the numbers only
     
    math.fox, May 29, 2010 IP
  5. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #5
    The following will test and extract the numbers:
    <?php
    
    $in = 'abcd1234';
    
    $r = array();
    if(preg_match('/^[a-z]+(\d+)$/', $in, $r)) {
    	echo "The numbers are $r[1]";
    }
    else
    	echo "String missing numbers or letters";
    
    ?>
    PHP:
    However I've based that on your example and so have assumed that the string always starts with 1 or more lowercase letters and ends with 1 or more numbers, any other pattern of letters and numbers will fail to match.

    If you have other patterns of letters and numbers then you'll need to show me them all so I can change that.
     
    Last edited: May 29, 2010
    tolra, May 29, 2010 IP
  6. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #6
    This will match if your string contains any upper/lowercase letter and a digit
    if (preg_match('/([a-z].*?[0-9]|[0-9].*?[a-z])/i', $input)) {
    	# Successful match
    } else {
    	# Match attempt failed
    }
    PHP:
     
    JAY6390, May 29, 2010 IP
  7. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #7
    However that does not extract the numbers from the string as the OP asked for.
     
    tolra, May 29, 2010 IP
  8. flexdex

    flexdex Peon

    Messages:
    104
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    How about that
    
    <?php
    $subject = '1234abcd5678efgh';
    $result = preg_split('/([0-9]+)|([a-z]+)/si', $subject, 0, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
    print_r($result);
    
    PHP:
     
    flexdex, May 29, 2010 IP
  9. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #9
    It doesn't verify that the input contains both letters and numbers it only extracts them.
     
    tolra, May 29, 2010 IP
  10. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #10
    danx10, May 29, 2010 IP
  11. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #11
    Kind of depends on the input pattern doesn't it, if all the input is in the same form as the example then a test and extract would seem the optimal solution.

    If the pattern is fixed as in the example then a preg_match will verify that the input is indeed in that form while ctype_alnum will not, so depending on where this is in the code, that is if this is data from a user or data a user can tamper with then a more strict type checking will limit or avoid issues with incorrectly input data and may increase security of the script.

    All boils down to what's expected and where the data comes from.
     
    tolra, May 29, 2010 IP
  12. flexdex

    flexdex Peon

    Messages:
    104
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Mate, consider the result array is empty. Then no letters or digits where present at the string. That is exactly what he needs. Read post #1 again.
     
    flexdex, May 30, 2010 IP
  13. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #13
    It won't be empty if there's digits only or letters only, he asked "how to check if a variable contains both numbers and letters ?", therefore while what you have extracts the data nicely it doesn't test that both letters and numbers are present, you'd need an additional check.
     
    tolra, May 30, 2010 IP