Results 1 to 6 of 6

Thread: Regex Help

  1. #1
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default Regex Help

    Hello, I can't seem to come up with a regular expression that will check to see if there are both letters AND numbers. I could only make it so it would check if it was Alphanumeric, but how can I make it so it will only return if BOTH letters and numbers are present:
    PHP Code:
        if(preg('[A-Za-z0-9]'$string))$message "Alphanumeric";
    echo 
    $message
    The above string will output if $string="123"; or $string="abc";. But I onlywant it to output if $string="abc123";
    What is the correct Regular Expression for this?
    Thanks,
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I'm not sure, but you could split the string in two and check them independently.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    I'm not sure what you mean, can you elaborate on how to do it. would you search the string for numbers and then search it for letters?
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Code:
    $string = 'aaa111';
    $s1 = substr($string,0,3);
    $s2 = substr($string,3);
    //$s1 = alpha
    //$s = numeric
    //also, strlen($s1) and strlen($s2) should == 3
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    I guess I was not entirely clear, the $string will be mixed numbers\letters like "1z3" or "98j" or "xy5" so I don't know where the letters\numbers are located in the string. I'm figuring I have to use two regular expressions on the string to pull out the letters and then the numbers. Then I have to check to see if these new strings are not null.
    ----
    AHAH! I think I solved it, haven't fully tested it yet but:
    PHP Code:
    $alpha=preg('[^A-Za-z]'$string);
    $numeric=preg('[^0-9]'$string);
    if(!
    $alpha || !$numeric$message="This string has numbers and variables"
    This works, but I wonder if there is any other function or regular express that will simplify this....
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  6. #6
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Code:
    if (preg_match('/\d/', $alpha_num)  && (preg_match('/[a-zA-Z]/', $alpha_num))) {
    A full example

    PHP Code:
    <form method="post" action="">
    <input type="text" name="alphanum" />
    <input type="submit" />
    </form>
    <?php
    if (isset($_POST['alphanum'])) {
    $alpha_num $_POST['alphanum'];
    echo 
    $alpha_num "<br />";
    if (
    preg_match('/\d/'$alpha_num)  && (preg_match('/[a-zA-Z]/'$alpha_num))) {
        echo 
    "alpha numeric";
    } else {
    echo 
    "not";
    }
    }
    ?>
    Corrections to my coding/thoughts welcome.

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
  •