Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post

    How do you allow only numeric characters in a string? ++ more..

    Ok...

    I know how you can make stuff lowercase like this..

    PHP Code:
    if (strtolower($var) == strtolower($var)) { 
    But how can you allow only numeric values in a string? Meaning if it comes back with a hypen or whitespace, brackets etc etc it will strip them out & leave ONLY numeric characters just like strtolower makes everything lowercase?

    Also, another quick question..

    How can you check if a string contains a certain character? So if I wanted to check that they put in a "@" I could check that the string contains one.

    Cheers!

  • #2
    New Coder
    Join Date
    May 2003
    Posts
    71
    Thanks
    0
    Thanked 0 Times in 0 Posts
    cpCommerce - Finally a template based open-source e-commerce solution

  • #3
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    That isn't going to work how I need it to work according to what I have read on that page.

    From the user comments something like this would seem to work better..

    PHP Code:
    eregi("[^0-9]",$var
    But I wouldn't have a clue what that means or how it works; I just know what it does lol

  • #4
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    I use this function for money variables, but you could use it for your purposes


    Code:
    function numpass_filter($userString){ 
    $money = explode(".", $userString);
    //next remove all characters save 0 though 9
    //in both elements of the array
    $dollars = eregi_replace("[^0-9]", null, $money[0]);
    $cents = eregi_replace("[^0-9]", null, $money[1]);
    
    //if there was a decimal in the original string, put it back
    if((string)$cents!=null){
    $cents = "." . $cents;
    }
    $result = $dollars . $cents;
    return($result);
    }

  • #5
    New Coder
    Join Date
    May 2003
    Posts
    71
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by cyphix
    That isn't going to work how I need it to work according to what I have read on that page.

    From the user comments something like this would seem to work better..

    PHP Code:
    eregi("[^0-9]",$var
    But I wouldn't have a clue what that means or how it works; I just know what it does lol

    Maybe I missed something, but could you tell me why is_numeric will not work for you? All it does is ensure the string you send it is all numbers....that is what you wanted right? Decimals are allowed...

    As for checking for a certain character:
    PHP Code:
    if (strpos($input"@") !== false) {
      
    // @ symbol was found
    } else {
      
    // @ symbol was not found

    You use !== versus != because it returns the location in the string of where the character was found, and if the first character is the '@' symbol then it would return a 0.

    -Matt
    cpCommerce - Finally a template based open-source e-commerce solution

  • #6
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    I believe he wanted the ability to strip out all non-numeric characters.
    Meaning if it comes back with a hypen or whitespace, brackets etc etc it will strip them out & leave ONLY numeric characters just like strtolower makes everything lowercase?
    Lite...

  • #7
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    How about...

    PHP Code:
    $string "943856.2523";
    if(
    preg_match("/[^0-9\.]/",$string)) {
       print 
    "this is not just numbers";
    }
    else {
       print 
    "only numbers";


  • #8
    New Coder
    Join Date
    May 2003
    Posts
    71
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by litebearer
    I believe he wanted the ability to strip out all non-numeric characters.


    Lite...
    Ah, so I did miss it. Sorry.
    cpCommerce - Finally a template based open-source e-commerce solution

  • #9
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Lite is correct cpradio.. I can't allow decimals.

    Thanks for the code to check for certain characters too.

    Lite.. thanks for sharing your code, but I'm not all sure how to edit it to work for me .. I've only been learning PHP for about a week now & some of that stuff is new to me.

    I had a look around php.net & came up with this, hope it works..

    It doesn't get rid of all non-numeric characters but replaces the more common chars people would input into phone fields.

    PHP Code:
    // Sets $ph to $phone so I can refer to $phone later & get the initial value
    $ph $phone;

    $ph_pattern[0] = "(";
    $ph_pattern[1] = ")";
    $ph_pattern[2] = "-";
    $ph_pattern[3] = ".";
    $ph_pattern[4] = " ";

    $ph_rep[4] = "";
    $ph_rep[3] = "";
    $ph_rep[2] = "";
    $ph_rep[1] = "";
    $ph_rep[0] = "";

    preg_replace($ph_pattern$ph_rep$ph);

    $sp_pattern[0] = "(";
    $sp_pattern[1] = ")";
    $sp_pattern[2] = "-";
    $sp_pattern[3] = ".";
    $sp_pattern[4] = " ";

    $sp_rep[4] = "";
    $sp_rep[3] = "";
    $sp_rep[2] = "";
    $sp_rep[1] = "";
    $sp_rep[0] = "";

    preg_replace($sp_pattern$sp_rep$searchphone); 

  • #10
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    um so what you want is...

    PHP Code:
    $str "(555) 123-4567";
    $clean preg_replace("/[^0-9]/","",$str); 
    In fact, if you're talking about phone numbers (in the us) they must be 3+3+4 = 10 or 1+3+3+4 = 11, so you could even do this...

    PHP Code:
    $str "(555) 123-4567";
    $clean preg_replace("/[^0-9]/","",$str);
    if(
    preg_match("/[0-9]{10,11}/",$clean)) {
       print 
    "Good";

    else {
       print 
    "Bad";


  • #11
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Thanks bcarl.. I'll try that.

    My code was getting errors ayway..

    Warning: No ending matching delimiter ')' found in xxxxxxxxxxxxx/search.php on line 158

    Warning: No ending delimiter ')' found in xxxxxxxxxxxxx/search.php on line 158

    Warning: No ending delimiter '-' found in xxxxxxxxxxxxx/search.php on line 158

    Warning: No ending delimiter '.' found in xxxxxxxxxxxxx/search.php on line 158

    Warning: Empty regular expression in xxxxxxxxxxxxx/search.php on line 158

    Warning: No ending delimiter '$' found in xxxxxxxxxxxxx/search.php on line 172

  • #12
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Worked good!


  •  

    Posting Permissions

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