Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

regex and php functions

Options
  • 06-09-2008 1:03pm
    #1
    Registered Users Posts: 788 ✭✭✭


    Is it possible to put regex into functions that you can call in php?

    eg.
    //checking if a number inserted in a form begins with 107 and has 6 numbers
    
     //ranging from 0-9 following that
     
    function is_number($number)
    {
    107^[0-9]{6}$;
    }
    


Comments

  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Do something like this:

    [php]
    function is_number($number)
    {
    if (preg_match ('/^107\d{3}$/', $number)) # Edit: no need for the +
    return true;

    return false;
    }


    if (is_number('107322'))
    print "Yes starts with 107"; # Prints this as it does
    else
    print "Nope";
    [/php]

    Clearly the length after the 107 should be 3 digits (\d is for digit) so that should do trick for you.


  • Registered Users Posts: 6,511 ✭✭✭daymobrew


    Webmonkey wrote:
    Do something like this:
    The code gave "Nope" for me - possibly because of the +.

    [php]<?php
    header("Content-type: text/plain");

    function is_number($number)
    {
    if (preg_match ('/^107\d{6}$/', $number)) // No + and uses 6.
    return true;

    return false;
    }

    $testlist = array( 101023, 103245666, 10712345, 107123456 );
    foreach ( $testlist as $numtotest ) {
    echo "$numtotest: ";
    if (is_number( $numtotest ))
    echo "Yes starts with 107 and has 9 digits.\n";
    else
    print "Nope\n";
    }
    ?>[/php]


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    And i'm getting a "Nope" with your one - I think your one needs to begin with 107 and have 6 digits following where as I think Meg means she wants the full thing to be 6 digits long.

    Mine might not have worked as the number of digits in my example is 7 as aposed to 6. I removed the + as well as no need for it :)


  • Registered Users Posts: 788 ✭✭✭sleepyescapade


    Thanks for both of your help :) yes I want to check if the next 6 digits range between 0-9, the entire thing would have 9 digits overall though.


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Ah right, cool - stick with daymobrew's version so ;)


  • Advertisement
  • Registered Users Posts: 788 ✭✭✭sleepyescapade


    Yay it works :) Just teaching myself regex at the moment and thought I'd make my own :)


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    megcork wrote: »
    Yay it works :) Just teaching myself regex at the moment and thought I'd make my own :)
    Good stuff :)


  • Registered Users Posts: 788 ✭✭✭sleepyescapade


    Hmm it doesnt actually work if I put a correct number that I want in though :(

    [php]
    is_number($number);
    if (! is_number($number))
    {
    $errors[] = 'Please enter a valid number';
    }
    [/php]


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    megcork wrote: »
    Hmm it doesnt actually work if I put a correct number that I want in though :(

    [php]
    is_number($number);
    if (! is_number($number))
    {
    $errors[] = 'Please enter a valid number';
    }
    [/php]
    Give example code/number - it should work.


  • Registered Users Posts: 788 ✭✭✭sleepyescapade


    [php]
    function is_number(107958383)
    {
    if (preg_match ('/^107\d{6}$/', 107958383))
    return true;

    return false;
    }
    [/php]


  • Advertisement
  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    How are you using the function.
    You should be using it like:

    [php]function is_number($number)
    {
    if (preg_match ('/^107\d{6}$/', $number))
    return true;

    return false;
    }

    if (is_number(107958383))
    print "yes";
    [/php]


  • Registered Users Posts: 788 ✭✭✭sleepyescapade


    Im using it in another folder called function.php and then i call it in my register.php user form.

    There is an input field in the form for user to enter a number. In the background I am running

    [php]
    <?php

    include('functions/function.php');
    $user_name = '';
    $number = '';

    is_number($number);
    if (! is_number($number))
    {
    $errors[] = 'Number not valid. Please check your number and try again.';
    }
    etc
    ?>
    [/php]

    If it's not a valid number it prints the above as an error.
    <p>
         <label for="numberField">Enter your Number:</label>
         <input type="text" name="number" id="numberField"
                size="10" maxlength="10" />
       </p>
    


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    I don't see why it doesn't work - I've tested here and fine. Whats happening - so even if user enters invalid number it still doesn't give the error?


  • Registered Users Posts: 788 ✭✭✭sleepyescapade


    No it gives the error even if a valid number is entered.


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    megcork wrote: »
    No it gives the error even if a valid number is entered.
    You arn't storing the error in a session by any chance, maybe it coming from somewhere else.

    Something else must be wrong because the function is perfect - you'll need to give more code I'm afraid :(


  • Registered Users Posts: 6,511 ✭✭✭daymobrew


    Webmonkey wrote: »
    And i'm getting a "Nope" with your one - I think your one needs to begin with 107 and have 6 digits following where as I think Meg means she wants the full thing to be 6 digits long.
    My code included test cases where only one was a valid number - it shows that both the true and false code paths work.


  • Closed Accounts Posts: 1,200 ✭✭✭louie


    I can't see why the functions above doesn't work.
    Did you try cleaning the string?
    [php]
    $x_number = $_POST;
    $x_number = trim($x_number);//remove white spaces before/after string
    $x_number = preg_replace("#[^0-9]#","",$x_number);//remove anything that is not a number
    if(strlen($x_number) == 9){ //call funtion
    if(!is_number($x_number)){//if function returns false
    $error_msg[] = "Number incorect";
    }
    }else{
    $error_msg[] = "Number incorect";
    }


    function is_number($number){
    $is_ok = false;//set to false
    if (preg_match ('/^107\d{6}$/', $number)){
    $is_ok = true;
    }else{
    $is_ok = false;
    }

    return $is_ok;
    }
    [/php]


  • Registered Users Posts: 788 ✭✭✭sleepyescapade


    louie wrote: »
    I can't see why the functions above doesn't work.
    Did you try cleaning the string?
    [php]
    $x_number = $_POST;
    $x_number = trim($x_number);//remove white spaces before/after string
    $x_number = preg_replace("#[^0-9]#","",$x_number);//remove anything that is not a number
    if(strlen($x_number) == 9){ //call funtion
    if(!is_number($x_number)){//if function returns false
    $error_msg[] = "Number incorect";
    }
    }else{
    $error_msg[] = "Number incorect";
    }


    function is_number($number){
    $is_ok = false;//set to false
    if (preg_match ('/^107\d{6}$/', $number)){
    $is_ok = true;
    }else{
    $is_ok = false;
    }

    return $is_ok;
    }
    [/php]

    got it working, had forgotten to put in $_POST, d'oh!


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    daymobrew wrote: »
    My code included test cases where only one was a valid number - it shows that both the true and false code paths work.
    Oh i know that :) It just didn't seem to work for me since I thought the total numbers was 6, 107 first along with a further 3.


  • Closed Accounts Posts: 1,200 ✭✭✭louie


    Is complicated to work something out while you don't have the entire code structure...
    and yes, "Webmonkey" code was right, but the fault lies with the OP as it was not mentioned 9# but only 6...


  • Advertisement
  • Registered Users Posts: 6,511 ✭✭✭daymobrew


    louie wrote: »
    but the fault lies with the OP as it was not mentioned 9# but only 6...
    OP did mention '107' followed by 6 numbers
    megcork wrote: »
    //checking if a number inserted in a form begins with 107 and has 6 numbers
    //ranging from 0-9 following that
    


  • Closed Accounts Posts: 1,200 ✭✭✭louie


    I am not going to argue with you on this but
    //checking if a number inserted in a form begins with 107 and has 6 numbers

    doesn't exactly says that should be followed by 6 numbers AFTER the 107...


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    daymobrew wrote: »
    OP did mention '107' followed by 6 numbers
    Why are we even discussing this? - The fact that the second comment was 2 lines down further made it look like it was a separate comment. Didn't even read it tbh.


Advertisement